Skip to contentSkip to conversation

Sound Open Firmware (SOF) core

PATCH Superseded Pierre-Louis Bossart 7 participants 95 messages Dec 11, 2018 – Jan 30, 2019 1 message not in the archive

Subsystems (3)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 cover Sound Open Firmware (SOF) core

Sound Open Firmware (SOF) is a host and DSP architecture agnostic audio DSP firmware. SOF is not tied to any specific host architecture or any specific physical IO communication type (it will work with on SoC DSPs, or DSP connected via SPI/I2C).

SOF is also not coupled to any particular DSP architecture and has abstraction similar to Linux to allow porting to other DSP architectures.

https://www.sofproject.org/

This patch series introduces the SOF core and utilities. Support for Intel devices is provided as a follow-up series.

The SOF core manages all the core DSP services and ALSA/ASoC IO. The core is responsible for loading firmware, parsing topology, exposing PCMs and kcontrols, providing debug and trace mechanisms and performing IPC between host and DSP.

The SOF core also has logic to allow reuse of existing machine drivers for other platforms/machines without any code modification. i.e. DAI links can be modified at runtime to bind with SOF and SOF topologies instead of existing hard coded DAI links and topology.

Main changes since v2 (end of August):

There are honestly too many changes to be listed... The biggest changes are the introduction of an ABI check and runtime_pm, as well as countless improvements coming from product validation. We will provide a revision history starting with the next update.

Precisions:

Since I've been maintaining the SOF kernel parts for most of 2018, Liam and I mutually agreed that it would be more efficient for me to send the patches upstream to avoid any disconnects. The code in this patchset is directly squashed from the SOF development branch [1], which tracks Mark Brown's for-next branch on a weekly basis and the configurations used for testing are based on the defconfigs at [2]

Full disclosure on known limitation and issues (full list at [3])

a) the interaction with ASoC is not always perfect, there are a couple of points where a better solution is desired (use of private data, support for link DMA, etc). The known points are explicitly documented in the code and will be updated. To be clearer, it's our belief that SOF does not cripple ASoC in any way, and that merging this SOF core does not harm others. It's just complicated to get things right, especially on a couple of cases where the topology and DPCM frameworks generate complex flows that very few people understand.
b) The get/put methods for controls generate an IPC and possibly a wake-up. This is not optimal and is being fixed.
c) The debugfs parts will be updated for runtime_pm parts
d) The topology free, firmware release and Hdaudio exit sequences will be updated. It is our intention to fully support module load/unload and free all resources.
e) runtime_pm is being hardened and the use of SMART_SUSPEND was suggested.

Thank you for reviews and comments, we appreciate the time spent commenting on this large patchset. Thanks in particular to Alan Cox and Andy Shevchenko for their comments on an earlier version.

Pierre

[1] https://github.com/thesofproject/linux
[2] https://github.com/thesofproject/kconfig
[3] https://github.com/thesofproject/linux/issues

Liam Girdwood (12):
ASoC: SOF: Add Sound Open Firmware driver core
ASoC: SOF: Add Sound Open Firmware KControl support
ASoC: SOF: Add driver debug support.
ASoC: SOF: Add support for IPC IO between DSP and Host
ASoC: SOF: Add PCM operations support
ASoC: SOF: Add support for loading topologies
ASoC: SOF: Add DSP firmware logger support
ASoC: SOF: Add DSP HW abstraction operations
ASoC: SOF: Add firmware loader support
ASoC: SOF: Add userspace ABI support
ASoC: SOF: Add PM support
ASoC: SOF: Add Nocodec machine driver support

Pierre-Louis Bossart (2):
ASoC: SOF: Add xtensa support
ASoC: SOF: Add utils

 include/sound/soc.h               |    3 +
 include/sound/sof.h               |   91 +
 include/sound/sof/control.h       |  125 ++
 include/sound/sof/dai-intel.h     |  175 ++
 include/sound/sof/dai.h           |   75 +
 include/sound/sof/header.h        |  158 ++
 include/sound/sof/info.h          |  118 ++
 include/sound/sof/pm.h            |   48 +
 include/sound/sof/stream.h        |  149 ++
 include/sound/sof/topology.h      |  256 +++
 include/sound/sof/trace.h         |   66 +
 include/sound/sof/xtensa.h        |   44 +
 include/uapi/sound/sof/abi.h      |   63 +
 include/uapi/sound/sof/eq.h       |  164 ++
 include/uapi/sound/sof/fw.h       |   67 +
 include/uapi/sound/sof/header.h   |   27 +
 include/uapi/sound/sof/manifest.h |  188 ++
 include/uapi/sound/sof/tokens.h   |   95 +
 include/uapi/sound/sof/tone.h     |   21 +
 include/uapi/sound/sof/trace.h    |   93 +
 sound/soc/sof/control.c           |  421 +++++
 sound/soc/sof/core.c              |  398 +++++
 sound/soc/sof/debug.c             |  176 ++
 sound/soc/sof/ipc.c               |  813 +++++++++
 sound/soc/sof/loader.c            |  322 ++++
 sound/soc/sof/nocodec.c           |   81 +
 sound/soc/sof/ops.c               |  200 +++
 sound/soc/sof/ops.h               |  379 ++++
 sound/soc/sof/pcm.c               |  755 ++++++++
 sound/soc/sof/pm.c                |  403 +++++
 sound/soc/sof/sof-priv.h          |  574 ++++++
 sound/soc/sof/topology.c          | 2775 +++++++++++++++++++++++++++++
 sound/soc/sof/trace.c             |  295 +++
 sound/soc/sof/utils.c             |  173 ++
 sound/soc/sof/xtensa/Kconfig      |    3 +
 sound/soc/sof/xtensa/Makefile     |    5 +
 sound/soc/sof/xtensa/core.c       |  152 ++
 37 files changed, 9951 insertions(+)
 create mode 100644 include/sound/sof.h
 create mode 100644 include/sound/sof/control.h
 create mode 100644 include/sound/sof/dai-intel.h
 create mode 100644 include/sound/sof/dai.h
 create mode 100644 include/sound/sof/header.h
 create mode 100644 include/sound/sof/info.h
 create mode 100644 include/sound/sof/pm.h
 create mode 100644 include/sound/sof/stream.h
 create mode 100644 include/sound/sof/topology.h
 create mode 100644 include/sound/sof/trace.h
 create mode 100644 include/sound/sof/xtensa.h
 create mode 100644 include/uapi/sound/sof/abi.h
 create mode 100644 include/uapi/sound/sof/eq.h
 create mode 100644 include/uapi/sound/sof/fw.h
 create mode 100644 include/uapi/sound/sof/header.h
 create mode 100644 include/uapi/sound/sof/manifest.h
 create mode 100644 include/uapi/sound/sof/tokens.h
 create mode 100644 include/uapi/sound/sof/tone.h
 create mode 100644 include/uapi/sound/sof/trace.h
 create mode 100644 sound/soc/sof/control.c
 create mode 100644 sound/soc/sof/core.c
 create mode 100644 sound/soc/sof/debug.c
 create mode 100644 sound/soc/sof/ipc.c
 create mode 100644 sound/soc/sof/loader.c
 create mode 100644 sound/soc/sof/nocodec.c
 create mode 100644 sound/soc/sof/ops.c
 create mode 100644 sound/soc/sof/ops.h
 create mode 100644 sound/soc/sof/pcm.c
 create mode 100644 sound/soc/sof/pm.c
 create mode 100644 sound/soc/sof/sof-priv.h
 create mode 100644 sound/soc/sof/topology.c
 create mode 100644 sound/soc/sof/trace.c
 create mode 100644 sound/soc/sof/utils.c
 create mode 100644 sound/soc/sof/xtensa/Kconfig
 create mode 100644 sound/soc/sof/xtensa/Makefile
 create mode 100644 sound/soc/sof/xtensa/core.c
Signature · 1 line
2.17.1
#1 lore raw

PATCH v3 1/14 ASoC: SOF: Add Sound Open Firmware driver core

From: Liam Girdwood <[email protected]>

The Sound Open Firmware driver core is a generic architecture independent layer that allows SOF to be used on many different different architectures and platforms. It abstracts DSP operations and IO methods so that the target DSP can be an internal memory mapped or external SPI or I2C based device. This abstraction also allows SOF to be run on many different VMs on the same physical HW.

SOF also requires some data in ASoC PCM runtime data for looking up SOF data during ASoC PCM operations.

---
 include/sound/soc.h      |   3 +
 include/sound/sof.h      |  81 ++++++
 sound/soc/sof/core.c     | 398 +++++++++++++++++++++++++++
 sound/soc/sof/sof-priv.h | 574 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 1056 insertions(+)
 create mode 100644 include/sound/sof.h
 create mode 100644 sound/soc/sof/core.c
 create mode 100644 sound/soc/sof/sof-priv.h
include/sound/soc.h +3 -0
index 8ec1de856ee7..45f1be5b7541 100644
@@ -1186,6 +1186,9 @@ struct snd_soc_pcm_runtime {
 	/* bit field */
 	unsigned int dev_registered:1;
 	unsigned int pop_wait:1;
+
+	/* private data - core does not touch */
+	void *private; /* FIXME: still SOF-specific, needs to less ambiguous */
 };
 #define for_each_rtd_codec_dai(rtd, i, dai)\
 	for ((i) = 0;						       \
include/sound/sof.h new file +81 -0

81 lines not on this page · raw message

sound/soc/sof/core.c new file +398 -0

398 lines not on this page · raw message

sound/soc/sof/sof-priv.h new file +574 -0

574 lines not on this page · raw message

Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH v3 01/14] ASoC: SOF: Add Sound Open Firmware driver core

Quoted 12 lines · Pierre-Louis Bossart

From: Liam Girdwood <[email protected]>

The Sound Open Firmware driver core is a generic architecture independent layer that allows SOF to be used on many different different architectures and platforms. It abstracts DSP operations and IO methods so that the target DSP can be an internal memory mapped or external SPI or I2C based device. This abstraction also allows SOF to be run on many different VMs on the same physical HW.

SOF also requires some data in ASoC PCM runtime data for looking up SOF data during ASoC PCM operations.

sound/soc/sof/core.c line 19

Quoted 3 lines
sound/soc/sof/core.c new file +3 -0
+/* SOF defaults if not provided by the platform in ms */
+#define TIMEOUT_DEFAULT_IPC	5
+#define TIMEOUT_DEFAULT_BOOT	100

Perhaps _MS at the end?

sound/soc/sof/core.c line 114

Quoted 12 lines
sound/soc/sof/core.c new file +11 -0
+struct snd_sof_dai *snd_sof_find_dai(struct snd_sof_dev *sdev,
+				     char *name)
+{
+	struct snd_sof_dai *dai = NULL;
+
+	list_for_each_entry(dai, &sdev->dai_list, list) {
 
+		if (!dai->name)
+			continue;
+
+		if (strcmp(name, dai->name) == 0)
+			return dai;

Perhaps

	if (dai->name && strcmp(...))
Quoted 9 lines
sound/soc/sof/core.c new file +8 -0
+	}
+
+	return NULL;
+}
 
+int snd_sof_create_page_table(struct snd_sof_dev *sdev,
+			      struct snd_dma_buffer *dmab,
+			      unsigned char *page_table, size_t size)
+{

sound/soc/sof/core.c line 203

Quoted 10 lines
sound/soc/sof/core.c new file +9 -0
+	int i, pages;
+
+	pages = snd_sgbuf_aligned_pages(size);
+
+	dev_dbg(sdev->dev, "generating page table for %p size 0x%zx pages %d\n",
+		dmab->area, size, pages);
+
+	for (i = 0; i < pages; i++) {
 
+		u32 idx = (((i << 2) + i)) >> 1;

Looks like

	u32 idx = (5 * i) >> 1;
Quoted 8 lines
sound/soc/sof/core.c new file +8 -0
+		u32 pfn = snd_sgbuf_get_addr(dmab, i * PAGE_SIZE) >> PAGE_SHIFT;
+		u32 *pg_table;
+
+		dev_vdbg(sdev->dev, "pfn i %i idx %d pfn %x\n", i, idx, pfn);
+
+		pg_table = (u32 *)(page_table + idx);
+
+		if (i & 1)

sound/soc/sof/core.c line 243

Quoted 10 lines
sound/soc/sof/core.c new file +9 -0
+			*pg_table |= (pfn << 4);
+		else
+			*pg_table |= pfn;
+	}
+
+	return pages;
+}
 
+	if (plat_data->type == SOF_DEVICE_PCI)
+		sdev->pci = container_of(plat_data->dev, struct pci_dev, dev);

Why not to use generic functions?

	dev_is_pci()
	to_pci_dev()

sound/soc/sof/core.c line 336

Quoted 4 lines
sound/soc/sof/core.c new file +4 -0
+	/* register machine driver, pass machine info as pdata */
+	plat_data->pdev_mach =
+		platform_device_register_data(sdev->dev, drv_name,
+					      -1, mach, size);

PLATFORM_DEVID_AUTO (IIRC the name)?

sound/soc/sof/core.c line 369

Quoted 8 lines
sound/soc/sof/core.c new file +7 -0
+static int sof_remove(struct platform_device *pdev)
+{
+	struct snd_sof_dev *sdev = dev_get_drvdata(&pdev->dev);
+	struct snd_sof_pdata *pdata = sdev->pdata;
+
 
+	if (pdata && !IS_ERR(pdata->pdev_mach))
+		platform_device_unregister(pdata->pdev_mach);

I'm wondering if pdata could be ever NULL here.
Also, as I mentioned internally the patch to accept error pointers would be in v4.21.

Quoted 5 lines
sound/soc/sof/core.c new file +5 -0
+
+	snd_soc_unregister_component(&pdev->dev);
+	snd_sof_fw_unload(sdev);
+	snd_sof_ipc_free(sdev);
+	snd_sof_free_debug(sdev);

sound/soc/sof/core.c line 383

Quoted 10 lines
sound/soc/sof/core.c new file +9 -0
+	snd_sof_free_trace(sdev);
+	snd_sof_remove(sdev);
+	return 0;
+}
+
 
+void snd_sof_shutdown(struct device *dev)
+{
+}
+EXPORT_SYMBOL(snd_sof_shutdown);

No need to provide an empty stub. Why not to add when it would have something useful?

sound/soc/sof/sof-priv.h line 36

Quoted 5 lines
sound/soc/sof/sof-priv.h new file +5 -0
+/* max BARs mmaped devices can use */
+#define SND_SOF_BARS	8
+
+/* time in ms for runtime suspend delay */
+#define SND_SOF_SUSPEND_DELAY	2000

_MS ?

Quoted 13 lines
sound/soc/sof/sof-priv.h new file +12 -0
+	struct mutex mutex;	/* access mutex */
+	struct list_head list;	/* list in sdev pcm list */
+	struct snd_pcm_hw_params params[2];
+	int restore_stream[2]; /* restore hw_params for paused stream */
 
+	u32 readback_offset; /* offset to mmaped data if used */
+	struct sof_ipc_ctrl_data *control_data;
+	u32 size;	/* cdata size */
+	enum sof_ipc_ctrl_cmd cmd;
+	u32 *volume_table; /* volume table computed from tlv data*/
+
+	struct mutex mutex;	/* access mutex */
+	struct list_head list;	/* list in sdev control list */
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

Re: [PATCH v3 01/14] ASoC: SOF: Add Sound Open Firmware driver core

Thanks for the late night review Andy :-)

Quoted 10 lines · Andy Shevchenko
Quoted 9 lines · Pierre-Louis Bossart

From: Liam Girdwood <[email protected]>

The Sound Open Firmware driver core is a generic architecture independent layer that allows SOF to be used on many different different architectures and platforms. It abstracts DSP operations and IO methods so that the target DSP can be an internal memory mapped or external SPI or I2C based device. This abstraction also allows SOF to be run on many different VMs on the same physical HW.

Quoted 6 lines

sound/soc/sof/core.c line 19

Quoted 5 lines

SOF also requires some data in ASoC PCM runtime data for looking up SOF data during ASoC PCM operations.

sound/soc/sof/core.c new file +3 -0
+/* SOF defaults if not provided by the platform in ms */
+#define TIMEOUT_DEFAULT_IPC	5
+#define TIMEOUT_DEFAULT_BOOT	100

Perhaps _MS at the end?

ok

Quoted 14 lines

sound/soc/sof/core.c line 114

Quoted 12 lines
sound/soc/sof/core.c new file +11 -0
+struct snd_sof_dai *snd_sof_find_dai(struct snd_sof_dev *sdev,
+				     char *name)
+{
+	struct snd_sof_dai *dai = NULL;
+
+	list_for_each_entry(dai, &sdev->dai_list, list) {
+		if (!dai->name)
+			continue;
+
+		if (strcmp(name, dai->name) == 0)
+			return dai;

Perhaps

Quoted 1 line
	if (dai->name && strcmp(...))

ok

Quoted 20 lines
Quoted 8 lines
sound/soc/sof/core.c new file +7 -0
+	}
+
+	return NULL;
+}
+int snd_sof_create_page_table(struct snd_sof_dev *sdev,
+			      struct snd_dma_buffer *dmab,
+			      unsigned char *page_table, size_t size)

sound/soc/sof/core.c line 203

Quoted 10 lines
sound/soc/sof/core.c new file +10 -0
+{
+	int i, pages;
+
+	pages = snd_sgbuf_aligned_pages(size);
+
+	dev_dbg(sdev->dev, "generating page table for %p size 0x%zx pages %d\n",
+		dmab->area, size, pages);
+
+	for (i = 0; i < pages; i++) {
+		u32 idx = (((i << 2) + i)) >> 1;

Looks like

Quoted 1 line
	u32 idx = (5 * i) >> 1;

Yes, but we'd also want an explanation of what we try to multiply by 2.5... Liam or Keyon, can you chime in?

Quoted 19 lines
Quoted 8 lines
sound/soc/sof/core.c new file +7 -0
+		u32 pfn = snd_sgbuf_get_addr(dmab, i * PAGE_SIZE) >> PAGE_SHIFT;
+		u32 *pg_table;
+
+		dev_vdbg(sdev->dev, "pfn i %i idx %d pfn %x\n", i, idx, pfn);
+
+		pg_table = (u32 *)(page_table + idx);
+

sound/soc/sof/core.c line 243

Quoted 10 lines
sound/soc/sof/core.c new file +10 -0
+		if (i & 1)
+			*pg_table |= (pfn << 4);
+		else
+			*pg_table |= pfn;
+	}
+
+	return pages;
+}
+	if (plat_data->type == SOF_DEVICE_PCI)
+		sdev->pci = container_of(plat_data->dev, struct pci_dev, dev);

Why not to use generic functions?

Quoted 2 lines
	dev_is_pci()
	to_pci_dev()

ok

Quoted 6 lines

sound/soc/sof/core.c line 336

Quoted 5 lines
sound/soc/sof/core.c new file +4 -0
+	/* register machine driver, pass machine info as pdata */
+	plat_data->pdev_mach =
+		platform_device_register_data(sdev->dev, drv_name,
+					      -1, mach, size);

PLATFORM_DEVID_AUTO (IIRC the name)?

did you mean replace -1 by PLATFORM_DEVID_NONE?

Quoted 9 lines

sound/soc/sof/core.c line 369

Quoted 8 lines
sound/soc/sof/core.c new file +7 -0
+static int sof_remove(struct platform_device *pdev)
+{
+	struct snd_sof_dev *sdev = dev_get_drvdata(&pdev->dev);
+	struct snd_sof_pdata *pdata = sdev->pdata;
+
+	if (pdata && !IS_ERR(pdata->pdev_mach))
+		platform_device_unregister(pdata->pdev_mach);

I'm wondering if pdata could be ever NULL here.

I didn't find an error flow that yields NULL but Liam reported some issues that made no sense so might be safer with NULL

Quoted 2 lines

Also, as I mentioned internally the patch to accept error pointers would be in v4.21.

Indeed, but I can't rely on this just yet.

Quoted 16 lines
Quoted 5 lines
sound/soc/sof/core.c new file +4 -0
+
+	snd_soc_unregister_component(&pdev->dev);
+	snd_sof_fw_unload(sdev);
+	snd_sof_ipc_free(sdev);

sound/soc/sof/core.c line 383

Quoted 10 lines
sound/soc/sof/core.c new file +10 -0
+	snd_sof_free_debug(sdev);
+	snd_sof_free_trace(sdev);
+	snd_sof_remove(sdev);
+	return 0;
+}
+
+void snd_sof_shutdown(struct device *dev)
+{
+}
+EXPORT_SYMBOL(snd_sof_shutdown);

No need to provide an empty stub. Why not to add when it would have something useful?

ok

Quoted 7 lines

sound/soc/sof/sof-priv.h line 36

Quoted 6 lines
sound/soc/sof/sof-priv.h new file +5 -0
+/* max BARs mmaped devices can use */
+#define SND_SOF_BARS	8
+
+/* time in ms for runtime suspend delay */
+#define SND_SOF_SUSPEND_DELAY	2000

_MS ?

ok

Quoted 13 lines
Quoted 13 lines
sound/soc/sof/sof-priv.h new file +12 -0
+	struct mutex mutex;	/* access mutex */
+	struct list_head list;	/* list in sdev pcm list */
+	struct snd_pcm_hw_params params[2];
+	int restore_stream[2]; /* restore hw_params for paused stream */
+	u32 readback_offset; /* offset to mmaped data if used */
+	struct sof_ipc_ctrl_data *control_data;
+	u32 size;	/* cdata size */
+	enum sof_ipc_ctrl_cmd cmd;
+	u32 *volume_table; /* volume table computed from tlv data*/
+
+	struct mutex mutex;	/* access mutex */
+	struct list_head list;	/* list in sdev control list */
#4 lore raw

Re: [PATCH v3 01/14] ASoC: SOF: Add Sound Open Firmware driver core

In reply to Pierre-Louis Bossart
Quoted 3 lines · Pierre-Louis Bossart
sound/soc/sof/core.c new file +2 -0
+static int sof_probe(struct platform_device *pdev)
+{

....

sound/soc/sof/core.c line 283

Quoted 7 lines
sound/soc/sof/core.c new file +7 -0
+	/* register any debug/trace capabilities */
+	ret = snd_sof_dbg_init(sdev);
+	if (ret < 0) {
+		dev_err(sdev->dev, "error: failed to init DSP trace/debug %d\n",
+			ret);
+		goto dbg_err;
+	}

So that's the problem Andy suggested.
snd_sof_dbg_init() returns an error for whatever reason, and this is considered as a fatal error and the probe fails. The error about debugfs is no fatal error, it should continue.

Moreover, as I mentioned for another patch (I read that before this one due to the mail delivery order :), it would fail always when CONFIG_DEBUGFS=n. At best, create a wrapper for CONFIG_DEBUGFS=n to return 0 (and void for free).

thanks,

Takashi

#5 lore raw

Re: [PATCH v3 01/14] ASoC: SOF: Add Sound Open Firmware driver core

Quoted 17 lines · Takashi Iwai
Quoted 2 lines · Pierre-Louis Bossart
sound/soc/sof/core.c new file +2 -0
+static int sof_probe(struct platform_device *pdev)
+{

....

sound/soc/sof/core.c line 283

Quoted 7 lines
sound/soc/sof/core.c new file +7 -0
+	/* register any debug/trace capabilities */
+	ret = snd_sof_dbg_init(sdev);
+	if (ret < 0) {
+		dev_err(sdev->dev, "error: failed to init DSP trace/debug %d\n",
+			ret);
+		goto dbg_err;
+	}

So that's the problem Andy suggested.
snd_sof_dbg_init() returns an error for whatever reason, and this is considered as a fatal error and the probe fails. The error about debugfs is no fatal error, it should continue.

Quoted 4 lines

Moreover, as I mentioned for another patch (I read that before this one due to the mail delivery order :), it would fail always when CONFIG_DEBUGFS=n. At best, create a wrapper for CONFIG_DEBUGFS=n to return 0 (and void for free).

ok. we'll fix this, point taken.

#6 lore raw

Re: [PATCH v3 01/14] ASoC: SOF: Add Sound Open Firmware driver core

In reply to Pierre-Louis Bossart

Thanks a lot Pierre, Liam and all the people at Intel for does such a great job with this project!

Few comments inline:

<snip>

include/sound/sof.h line 23

Quoted 5 lines
include/sound/sof.h new file +5 -0
+/* SOF probe type */
+enum sof_device_type {
+       SOF_DEVICE_PCI = 0,
+       SOF_DEVICE_APCI,
+       SOF_DEVICE_SPI

Maybe comma after last element here? We have at least one more: SOF_DEVICE_DT.

Quoted 38 lines
sound/soc/sof/core.c new file +38 -0
+static int sof_probe(struct platform_device *pdev)
+{
+       struct snd_sof_pdata *plat_data = dev_get_platdata(&pdev->dev);
+       struct snd_sof_dev *sdev;
+       const char *drv_name;
+       const void *mach;
+       int size;
+       int ret;
+
+       sdev = devm_kzalloc(&pdev->dev, sizeof(*sdev), GFP_KERNEL);
+       if (!sdev)
+               return -ENOMEM;
+
+       dev_dbg(&pdev->dev, "probing SOF DSP device....\n");
+
+       /* initialize sof device */
+       sdev->dev = &pdev->dev;
+       sdev->parent = plat_data->dev;
+       if (plat_data->type == SOF_DEVICE_PCI)
+               sdev->pci = container_of(plat_data->dev, struct pci_dev, dev);
+       sdev->ops = plat_data->machine->pdata;
+
+       sdev->pdata = plat_data;
+       sdev->first_boot = true;
+       INIT_LIST_HEAD(&sdev->pcm_list);
+       INIT_LIST_HEAD(&sdev->kcontrol_list);
+       INIT_LIST_HEAD(&sdev->widget_list);
+       INIT_LIST_HEAD(&sdev->dai_list);
+       INIT_LIST_HEAD(&sdev->route_list);
+       dev_set_drvdata(&pdev->dev, sdev);
+       spin_lock_init(&sdev->ipc_lock);
+       spin_lock_init(&sdev->hw_lock);
+
+       /* set up platform component driver */
+       snd_sof_new_platform_drv(sdev);
+
+       /* set default timeouts if none provided */
+       if (plat_data->desc->ipc_timeout == 0)

sound/soc/sof/core.c line 271

Quoted 10 lines
sound/soc/sof/core.c new file +10 -0
+               sdev->ipc_timeout = TIMEOUT_DEFAULT_IPC;
+       else
+               sdev->ipc_timeout = plat_data->desc->ipc_timeout;
+       if (plat_data->desc->boot_timeout == 0)
+               sdev->boot_timeout = TIMEOUT_DEFAULT_BOOT;
+       else
+               sdev->boot_timeout = plat_data->desc->boot_timeout;
+
+       /* probe the DSP hardware */
+       ret = snd_sof_probe(sdev);

Something looks fishy here.

I couldn't find this function's definition anywhere in the first patch which means that after first patch the code doesn't compile :D.

For bisection reasons would be great if the code compiles after each patch.

<snip>

sound/soc/sof/core.c line 326

Quoted 8 lines
sound/soc/sof/core.c new file +8 -0
+       /* now register audio DSP platform driver and dai */
+       ret = snd_soc_register_component(&pdev->dev,  &sdev->plat_drv,
+                                        sdev->ops->drv,
+                                        sdev->ops->num_drv);
+       if (ret < 0) {
+               dev_err(sdev->dev,
+                       "error: failed to register DSP DAI driver %d\n", ret);
+               goto comp_err;

This goes to snd_soc_unregister_component which is not really needed because the component didn't register succcesfully.

<snd>

sound/soc/sof/core.c line 350

Quoted 3 lines
sound/soc/sof/core.c new file +3 -0
+comp_err:
+       snd_soc_unregister_component(&pdev->dev);
+       snd_sof_free_topology(sdev);

It is not very clear to me where does the snd_sof_load_topology it is called so that there is a need to free it here.

Quoted 4 lines
sound/soc/sof/core.c new file +4 -0
+static int sof_remove(struct platform_device *pdev)
+{
+       struct snd_sof_dev *sdev = dev_get_drvdata(&pdev->dev);
+       struct snd_sof_pdata *pdata = sdev->pdata;

sound/soc/sof/core.c line 376

Quoted 10 lines
sound/soc/sof/core.c new file +10 -0
+
+       if (pdata && !IS_ERR(pdata->pdev_mach))
+               platform_device_unregister(pdata->pdev_mach);
+
+       snd_soc_unregister_component(&pdev->dev);
+       snd_sof_fw_unload(sdev);
+       snd_sof_ipc_free(sdev);
+       snd_sof_free_debug(sdev);
+       snd_sof_free_trace(sdev);
+       snd_sof_remove(sdev);

There is no snd_sof_free_topology here. Which means that either this call is not needed in the
probe function or it is missing from here.

sound/soc/sof/core.c line 383

Quoted 4 lines
sound/soc/sof/core.c new file +4 -0
+void snd_sof_shutdown(struct device *dev)
+{
+}
+EXPORT_SYMBOL(snd_sof_shutdown);

I would avoid this empty function calls for the moment. Will add it later when there is a need for it.

<snip>

Quoted 12 lines
sound/soc/sof/sof-priv.h new file +12 -0
+/*
+ * SOF Device Level.
+ */
+struct snd_sof_dev {
+       struct device *dev;
+       struct device *parent;
+       spinlock_t ipc_lock;    /* lock for IPC users */
+       spinlock_t hw_lock;     /* lock for HW IO access */
+       struct pci_dev *pci;
+
+       /* ASoC components */
+       struct snd_soc_component_driver plat_drv;

sound/soc/sof/sof-priv.h line 318

Quoted 10 lines
sound/soc/sof/sof-priv.h new file +10 -0
+
+       /* DSP firmware boot */
+       wait_queue_head_t boot_wait;
+       u32 boot_complete;
+       u32 first_boot;
+
+       /* DSP HW differentiation */
+       struct snd_sof_pdata *pdata;
+       const struct snd_sof_dsp_ops *ops;
+       struct sof_intel_hda_dev *hda;  /* for HDA based DSP HW */

Just a design question. Is it ok to hold SOC specific data here? For example on arm there is no HDA but there is some other DAI. Would it be ok for me to just add it here?

thanks,
Daniel.

#7 lore raw

Re: [PATCH v3 01/14] ASoC: SOF: Add Sound Open Firmware driver core

Hi Daniel,

Quoted 2 lines · Daniel Baluta

Thanks a lot Pierre, Liam and all the people at Intel for does such a great job with this project!

you're welcome, thanks for the comments

Quoted 11 lines

Few comments inline:

<snip>

include/sound/sof.h line 23

Quoted 5 lines
include/sound/sof.h new file +5 -0
+/* SOF probe type */
+enum sof_device_type {
+       SOF_DEVICE_PCI = 0,
+       SOF_DEVICE_APCI,
+       SOF_DEVICE_SPI

Maybe comma after last element here? We have at least one more: SOF_DEVICE_DT.

Based on the comments from Andy, we've already removed this type since it's not really needed any longer. We would probably need to have a sof-dt-dev.c file to deal with the different enumeration style but we'd need a platform to work with (or patches welcome). Hint hint wink wink.

Quoted 51 lines
Quoted 39 lines
sound/soc/sof/core.c new file +38 -0
+static int sof_probe(struct platform_device *pdev)
+{
+       struct snd_sof_pdata *plat_data = dev_get_platdata(&pdev->dev);
+       struct snd_sof_dev *sdev;
+       const char *drv_name;
+       const void *mach;
+       int size;
+       int ret;
+
+       sdev = devm_kzalloc(&pdev->dev, sizeof(*sdev), GFP_KERNEL);
+       if (!sdev)
+               return -ENOMEM;
+
+       dev_dbg(&pdev->dev, "probing SOF DSP device....\n");
+
+       /* initialize sof device */
+       sdev->dev = &pdev->dev;
+       sdev->parent = plat_data->dev;
+       if (plat_data->type == SOF_DEVICE_PCI)
+               sdev->pci = container_of(plat_data->dev, struct pci_dev, dev);
+       sdev->ops = plat_data->machine->pdata;
+
+       sdev->pdata = plat_data;
+       sdev->first_boot = true;
+       INIT_LIST_HEAD(&sdev->pcm_list);
+       INIT_LIST_HEAD(&sdev->kcontrol_list);
+       INIT_LIST_HEAD(&sdev->widget_list);
+       INIT_LIST_HEAD(&sdev->dai_list);
+       INIT_LIST_HEAD(&sdev->route_list);
+       dev_set_drvdata(&pdev->dev, sdev);
+       spin_lock_init(&sdev->ipc_lock);
+       spin_lock_init(&sdev->hw_lock);
+
+       /* set up platform component driver */
+       snd_sof_new_platform_drv(sdev);
+
+       /* set default timeouts if none provided */
+       if (plat_data->desc->ipc_timeout == 0)

sound/soc/sof/core.c line 271

Quoted 10 lines
sound/soc/sof/core.c new file +10 -0
+               sdev->ipc_timeout = TIMEOUT_DEFAULT_IPC;
+       else
+               sdev->ipc_timeout = plat_data->desc->ipc_timeout;
+       if (plat_data->desc->boot_timeout == 0)
+               sdev->boot_timeout = TIMEOUT_DEFAULT_BOOT;
+       else
+               sdev->boot_timeout = plat_data->desc->boot_timeout;
+
+       /* probe the DSP hardware */
+       ret = snd_sof_probe(sdev);

Something looks fishy here.

Quoted 4 lines

I couldn't find this function's definition anywhere in the first patch which means that after first patch the code doesn't compile :D.

For bisection reasons would be great if the code compiles after each patch.

the build is only added last (in the 37th patch). We didn't really plan to be able to compile the initial patches one after the other. we *could* add the core compilation earlier and add support for each device incrementally, but you need a set of files that are somewhat interdependent first.

Quoted 11 lines

<snip>

sound/soc/sof/core.c line 326

Quoted 8 lines
sound/soc/sof/core.c new file +8 -0
+       /* now register audio DSP platform driver and dai */
+       ret = snd_soc_register_component(&pdev->dev,  &sdev->plat_drv,
+                                        sdev->ops->drv,
+                                        sdev->ops->num_drv);
+       if (ret < 0) {
+               dev_err(sdev->dev,
+                       "error: failed to register DSP DAI driver %d\n", ret);
+               goto comp_err;
Quoted 2 lines

This goes to snd_soc_unregister_component which is not really needed because the component didn't register succcesfully.

Right. we went through a series of checks for error flows and missed this one. Thanks for reporting this.

Quoted 8 lines

<snd>

sound/soc/sof/core.c line 350

Quoted 3 lines
sound/soc/sof/core.c new file +3 -0
+comp_err:
+       snd_soc_unregister_component(&pdev->dev);
+       snd_sof_free_topology(sdev);

It is not very clear to me where does the snd_sof_load_topology it is called so that there is a need to free it here.

the free_topology part is something we are currently looking it. It seems that the framework frees almost everything. It's one of the difficulties we are facing, it's not always obvious what the topology framework does for you and what needs to be freed manually.  It's a known open we will fix shortly.

Quoted 10 lines
Quoted 10 lines
sound/soc/sof/core.c new file +9 -0
+static int sof_remove(struct platform_device *pdev)
+{
+       struct snd_sof_dev *sdev = dev_get_drvdata(&pdev->dev);
+       struct snd_sof_pdata *pdata = sdev->pdata;
+
+       if (pdata && !IS_ERR(pdata->pdev_mach))
+               platform_device_unregister(pdata->pdev_mach);
+
+       snd_soc_unregister_component(&pdev->dev);
Quoted 8 lines

sound/soc/sof/core.c line 376

Quoted 5 lines
sound/soc/sof/core.c new file +5 -0
+       snd_sof_fw_unload(sdev);
+       snd_sof_ipc_free(sdev);
+       snd_sof_free_debug(sdev);
+       snd_sof_free_trace(sdev);
+       snd_sof_remove(sdev);

There is no snd_sof_free_topology here. Which means that either this call is not needed in the
probe function or it is missing from here.

The topology is leaded in sof_pcm_probe and released in sof_pcm_remove.

But like I said above it's not clear if we need to really free anything...

Quoted 7 lines

sound/soc/sof/core.c line 383

Quoted 5 lines
sound/soc/sof/core.c new file +4 -0
+void snd_sof_shutdown(struct device *dev)
+{
+}
+EXPORT_SYMBOL(snd_sof_shutdown);

I would avoid this empty function calls for the moment. Will add it later when there is a need for it.

yes, we removed it already.

Quoted 20 lines

<snip>

Quoted 17 lines
sound/soc/sof/sof-priv.h new file +17 -0
+/*
+ * SOF Device Level.
+ */
+struct snd_sof_dev {
+       struct device *dev;
+       struct device *parent;
+       spinlock_t ipc_lock;    /* lock for IPC users */
+       spinlock_t hw_lock;     /* lock for HW IO access */
+       struct pci_dev *pci;
+
+       /* ASoC components */
+       struct snd_soc_component_driver plat_drv;
+
+       /* DSP firmware boot */
+       wait_queue_head_t boot_wait;
+       u32 boot_complete;
+       u32 first_boot;
Quoted 8 lines

sound/soc/sof/sof-priv.h line 318

Quoted 5 lines
sound/soc/sof/sof-priv.h new file +5 -0
+
+       /* DSP HW differentiation */
+       struct snd_sof_pdata *pdata;
+       const struct snd_sof_dsp_ops *ops;
+       struct sof_intel_hda_dev *hda;  /* for HDA based DSP HW */

Just a design question. Is it ok to hold SOC specific data here? For example on arm there is no HDA but there is some other DAI. Would it be ok for me to just add it here?

Darn, this was added more than a year ago and should have been cleaned-up. There is absolutely no reason why we have an Intel-specific definition in there, as you mentioned it it should have been a pointer to SoC-specific data. we'll fix this. nice catch, much appreciated.

-Pierre

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#8 lore raw

Re: [PATCH v3 01/14] ASoC: SOF: Add Sound Open Firmware driver core

In reply to Pierre-Louis Bossart

<snip>

Quoted 1 line
sound/soc/sof/core.c +0 -0

<snip>

sound/soc/sof/core.c

Quoted 5 lines
sound/soc/sof/core.c +5 -0
+
+/* SOF defaults if not provided by the platform in ms */
+
+       /* firmware loader */
+       int cl_bar;

This cl_bar variable seems to be only assigned to but never used.

I propose to remove it if there is no much trouble.

#9 lore raw

Re: [PATCH v3 01/14] ASoC: SOF: Add Sound Open Firmware driver core

Quoted 5 lines · Daniel Baluta

<snip>

Quoted 1 line
sound/soc/sof/core.c +0 -0

<snip>

Quoted 8 lines

sound/soc/sof/core.c

Quoted 5 lines
sound/soc/sof/core.c +5 -0
+
+/* SOF defaults if not provided by the platform in ms */
+
+       /* firmware loader */
+       int cl_bar;

This cl_bar variable seems to be only assigned to but never used.

I propose to remove it if there is no much trouble.

Indeed, not sure what it's supposed to be used for, it was part of the initial commit for all the files below and never used. If it's not been used in a year it's probably useless, I'll remove it.

git grep cl_bar

intel/bdw.c:    sdev->cl_bar = BDW_DSP_BAR; intel/byt.c:    sdev->cl_bar = BYT_DSP_BAR; intel/byt.c:    sdev->cl_bar = BYT_DSP_BAR; intel/hsw.c:    sdev->cl_bar = HSW_DSP_BAR; sof-priv.h:     int cl_bar;

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#10 lore raw

PATCH v3 2/14 ASoC: SOF: Add Sound Open Firmware KControl support

In reply to Pierre-Louis Bossart

From: Liam Girdwood <[email protected]>

SOF exposes regular ALSA Kcontrols that are defined by topology. This patch converts the Kcontrol IO to DSP IPC.

The current implementation is not optimal and can be improved:
a) for every get/put the host wakes up the DSP and generates an IPC. The kernel should cache the values and generate an IPC only when strictly necessary.
b) the firmware can be implemented to only instantiate the pipelines and related control-related parts that are needed at a given time, and power-gate the relevant SRAM blocks.

The development tasks for these two improvements has started, once validated they willl be provided in an update.

---
 sound/soc/sof/control.c | 421 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 421 insertions(+)
 create mode 100644 sound/soc/sof/control.c
sound/soc/sof/control.c new file +421 -0
new file mode 100644
index 000000000000..2000f62e931b
@@ -0,0 +1,421 @@
+// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
+//
+// This file is provided under a dual BSD/GPLv2 license.  When using or
+// redistributing this file, you may do so under either license.
+//
+// Copyright(c) 2018 Intel Corporation. All rights reserved.
+//
+// Author: Liam Girdwood <[email protected]>
+//
+
+/* Mixer Controls */
+
+#include <linux/pm_runtime.h>
+#include "sof-priv.h"
+
+static inline u32 mixer_to_ipc(unsigned int value, u32 *volume_map, int size)
+{
+	if (value >= size)
+		return volume_map[size - 1];
+
+	return volume_map[value];
+}
+
+static inline u32 ipc_to_mixer(u32 value, u32 *volume_map, int size)
+{
+	int i;
+
+	for (i = 0; i < size; i++) {
+		if (volume_map[i] >= value)
+			return i;
+	}
+
+	return i - 1;
+}
+
+int snd_sof_volume_get(struct snd_kcontrol *kcontrol,
+		       struct snd_ctl_elem_value *ucontrol)
+{
+	struct soc_mixer_control *sm =
+		(struct soc_mixer_control *)kcontrol->private_value;
+	struct snd_sof_control *scontrol = sm->dobj.private;
+	struct snd_sof_dev *sdev = scontrol->sdev;
+	struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
+	unsigned int i, channels = scontrol->num_channels;
+	int err, ret;
+
+	ret = pm_runtime_get_sync(sdev->dev);
+	if (ret < 0) {
+		dev_err(sdev->dev, "error: volume get failed to resume %d\n",
+			ret);
+		return ret;
+	}
+
+	/* get all the mixer data from DSP */
+	snd_sof_ipc_get_comp_data(sdev->ipc, scontrol, SOF_IPC_COMP_GET_VALUE,
+				  SOF_CTRL_TYPE_VALUE_CHAN_GET,
+				  SOF_CTRL_CMD_VOLUME);
+
+	/* read back each channel */
+	for (i = 0; i < channels; i++)
+		ucontrol->value.integer.value[i] =
+			ipc_to_mixer(cdata->chanv[i].value,
+				     scontrol->volume_table, sm->max + 1);
+
+	pm_runtime_mark_last_busy(sdev->dev);
+	err = pm_runtime_put_autosuspend(sdev->dev);
+	if (err < 0)
+		dev_err(sdev->dev, "error: volume get failed to idle %d\n",
+			err);
+	return 0;
+}
+
+int snd_sof_volume_put(struct snd_kcontrol *kcontrol,
+		       struct snd_ctl_elem_value *ucontrol)
+{
+	struct soc_mixer_control *sm =
+		(struct soc_mixer_control *)kcontrol->private_value;
+	struct snd_sof_control *scontrol = sm->dobj.private;
+	struct snd_sof_dev *sdev = scontrol->sdev;
+	struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
+	unsigned int i, channels = scontrol->num_channels;
+	int ret, err;
+
+	ret = pm_runtime_get_sync(sdev->dev);
+	if (ret < 0) {
+		dev_err(sdev->dev, "error: volume put failed to resume %d\n",
+			ret);
+		return ret;
+	}
+
+	/* update each channel */
+	for (i = 0; i < channels; i++) {
+		cdata->chanv[i].value =
+			mixer_to_ipc(ucontrol->value.integer.value[i],
+				     scontrol->volume_table, sm->max + 1);
+		cdata->chanv[i].channel = i;
+	}
+
+	/* notify DSP of mixer updates */
+	snd_sof_ipc_set_comp_data(sdev->ipc, scontrol, SOF_IPC_COMP_SET_VALUE,
+				  SOF_CTRL_TYPE_VALUE_CHAN_GET,
+				  SOF_CTRL_CMD_VOLUME);
+
+	pm_runtime_mark_last_busy(sdev->dev);
+	err = pm_runtime_put_autosuspend(sdev->dev);
+	if (err < 0)
+		dev_err(sdev->dev, "error: volume put failed to idle %d\n",
+			err);
+	return 0;
+}
+
+int snd_sof_enum_get(struct snd_kcontrol *kcontrol,
+		     struct snd_ctl_elem_value *ucontrol)
+{
+	struct soc_enum *se =
+		(struct soc_enum *)kcontrol->private_value;
+	struct snd_sof_control *scontrol = se->dobj.private;
+	struct snd_sof_dev *sdev = scontrol->sdev;
+	struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
+	unsigned int i, channels = scontrol->num_channels;
+	int err, ret;
+
+	ret = pm_runtime_get_sync(sdev->dev);
+	if (ret < 0) {
+		dev_err(sdev->dev, "error: enum get failed to resume %d\n",
+			ret);
+		return ret;
+	}
+
+	/* get all the mixer data from DSP */
+	snd_sof_ipc_get_comp_data(sdev->ipc, scontrol, SOF_IPC_COMP_GET_VALUE,
+				  SOF_CTRL_TYPE_VALUE_CHAN_GET,
+				  SOF_CTRL_CMD_ENUM);
+
+	/* read back each channel */
+	for (i = 0; i < channels; i++)
+		ucontrol->value.integer.value[i] = cdata->chanv[i].value;
+
+	pm_runtime_mark_last_busy(sdev->dev);
+	err = pm_runtime_put_autosuspend(sdev->dev);
+	if (err < 0)
+		dev_err(sdev->dev, "error: enum get failed to idle %d\n",
+			ret);
+	return 0;
+}
+
+int snd_sof_enum_put(struct snd_kcontrol *kcontrol,
+		     struct snd_ctl_elem_value *ucontrol)
+{
+	struct soc_enum *se =
+		(struct soc_enum *)kcontrol->private_value;
+	struct snd_sof_control *scontrol = se->dobj.private;
+	struct snd_sof_dev *sdev = scontrol->sdev;
+	struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
+	unsigned int i, channels = scontrol->num_channels;
+	int ret, err;
+
+	ret = pm_runtime_get_sync(sdev->dev);
+	if (ret < 0) {
+		dev_err(sdev->dev, "error: enum put failed to resume %d\n",
+			ret);
+		return ret;
+	}
+
+	/* update each channel */
+	for (i = 0; i < channels; i++)
+		cdata->chanv[i].value = ucontrol->value.integer.value[i];
+
+	/* notify DSP of mixer updates */
+	snd_sof_ipc_set_comp_data(sdev->ipc, scontrol, SOF_IPC_COMP_SET_VALUE,
+				  SOF_CTRL_TYPE_VALUE_CHAN_SET,
+				  SOF_CTRL_CMD_ENUM);
+
+	pm_runtime_mark_last_busy(sdev->dev);
+	err = pm_runtime_put_autosuspend(sdev->dev);
+	if (err < 0)
+		dev_err(sdev->dev, "error: enum put failed to idle %d\n",
+			err);
+	return 0;
+}
+
+int snd_sof_bytes_get(struct snd_kcontrol *kcontrol,
+		      struct snd_ctl_elem_value *ucontrol)
+{
+	struct soc_bytes_ext *be =
+		(struct soc_bytes_ext *)kcontrol->private_value;
+	struct snd_sof_control *scontrol = be->dobj.private;
+	struct snd_sof_dev *sdev = scontrol->sdev;
+	struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
+	struct sof_abi_hdr *data = cdata->data;
+	size_t size;
+	int ret, err;
+
+	ret = pm_runtime_get_sync(sdev->dev);
+	if (ret < 0) {
+		dev_err(sdev->dev, "error: bytes get failed to resume %d\n",
+			ret);
+		return ret;
+	}
+
+	/* get all the mixer data from DSP */
+	snd_sof_ipc_get_comp_data(sdev->ipc, scontrol, SOF_IPC_COMP_GET_DATA,
+				  SOF_CTRL_TYPE_DATA_GET, scontrol->cmd);
+	size = data->size + sizeof(*data);
+	if (size > be->max) {
+		dev_err(sdev->dev, "error: DSP sent %zu bytes max is %d\n",
+			size, be->max);
+		ret = -EINVAL;
+		goto out;
+	}
+
+	/* copy back to kcontrol */
+	memcpy(ucontrol->value.bytes.data, data, size);
+
+out:
+	pm_runtime_mark_last_busy(sdev->dev);
+	err = pm_runtime_put_autosuspend(sdev->dev);
+	if (err < 0)
+		dev_err(sdev->dev, "error: bytes get failed to idle %d\n",
+			err);
+	return ret;
+}
+
+int snd_sof_bytes_put(struct snd_kcontrol *kcontrol,
+		      struct snd_ctl_elem_value *ucontrol)
+{
+	struct soc_bytes_ext *be =
+		(struct soc_bytes_ext *)kcontrol->private_value;
+	struct snd_sof_control *scontrol = be->dobj.private;
+	struct snd_sof_dev *sdev = scontrol->sdev;
+	struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
+	struct sof_abi_hdr *data = cdata->data;
+	int ret, err;
+
+	ret = pm_runtime_get_sync(sdev->dev);
+	if (ret < 0) {
+		dev_err(sdev->dev, "error: bytes put failed to resume %d\n",
+			ret);
+		return ret;
+	}
+
+	if (data->size > be->max) {
+		dev_err(sdev->dev, "error: size too big %d bytes max is %d\n",
+			data->size, be->max);
+		ret = -EINVAL;
+		goto out;
+	}
+
+	/* copy from kcontrol */
+	memcpy(data, ucontrol->value.bytes.data, data->size);
+
+	/* notify DSP of mixer updates */
+	snd_sof_ipc_set_comp_data(sdev->ipc, scontrol, SOF_IPC_COMP_SET_DATA,
+				  SOF_CTRL_TYPE_DATA_SET, scontrol->cmd);
+
+out:
+	pm_runtime_mark_last_busy(sdev->dev);
+	err = pm_runtime_put_autosuspend(sdev->dev);
+	if (err < 0)
+		dev_err(sdev->dev, "error: bytes put failed to idle %d\n",
+			err);
+	return ret;
+}
+
+int snd_sof_bytes_ext_put(struct snd_kcontrol *kcontrol,
+			  const unsigned int __user *binary_data,
+			  unsigned int size)
+{
+	struct soc_bytes_ext *be =
+		(struct soc_bytes_ext *)kcontrol->private_value;
+	struct snd_sof_control *scontrol = be->dobj.private;
+	struct snd_sof_dev *sdev = scontrol->sdev;
+	struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
+	struct snd_ctl_tlv header;
+	struct snd_ctl_tlv __user *tlvd =
+		(struct snd_ctl_tlv __user *)binary_data;
+	int ret;
+	int err;
+	int max_size = SOF_IPC_MSG_MAX_SIZE -
+		sizeof(const struct sof_ipc_ctrl_data);
+
+	ret = pm_runtime_get_sync(sdev->dev);
+	if (ret < 0) {
+		dev_err(sdev->dev, "error: bytes_ext put failed to resume %d\n",
+			ret);
+		return ret;
+	}
+
+	/* The beginning of bytes data contains a header from where
+	 * the length (as bytes) is needed to know the correct copy
+	 * length of data from tlvd->tlv.
+	 */
+	if (copy_from_user(&header, tlvd, sizeof(const struct snd_ctl_tlv))) {
+		ret = -EFAULT;
+		goto out;
+	}
+	/* The maximum length that can be copied is limited by IPC max
+	 * length and topology defined length for ext bytes control.
+	 */
+	max_size = (be->max < max_size) ? be->max : max_size;
+	if (header.length > max_size) {
+		dev_err(sdev->dev, "error: Bytes data size %d exceeds max %d.\n",
+			header.length, max_size);
+		ret = -EINVAL;
+		goto out;
+	}
+
+	/* Check that header id matches the command */
+	if (header.numid != scontrol->cmd) {
+		dev_err(sdev->dev, "error: incorrect numid %d\n", header.numid);
+		ret = -EINVAL;
+		goto out;
+	}
+
+	if (copy_from_user(cdata->data, tlvd->tlv, header.length)) {
+		ret = -EFAULT;
+		goto out;
+	}
+
+	if (cdata->data->magic != SOF_ABI_MAGIC) {
+		dev_err(sdev->dev, "error: Wrong ABI magic 0x%08x.\n",
+			cdata->data->magic);
+		ret = -EINVAL;
+		goto out;
+	}
+
+	if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, cdata->data->abi)) {
+		dev_err(sdev->dev, "error: Incompatible ABI version 0x%08x.\n",
+			cdata->data->abi);
+		ret = -EINVAL;
+		goto out;
+	}
+
+	if (cdata->data->size + sizeof(const struct sof_abi_hdr) > max_size) {
+		dev_err(sdev->dev, "error: Mismatch in ABI data size (truncated?).\n");
+		ret = -EINVAL;
+		goto out;
+	}
+
+	/* notify DSP of mixer updates */
+	snd_sof_ipc_set_comp_data(sdev->ipc, scontrol, SOF_IPC_COMP_SET_DATA,
+				  SOF_CTRL_TYPE_DATA_SET, scontrol->cmd);
+
+out:
+	pm_runtime_mark_last_busy(sdev->dev);
+	err = pm_runtime_put_autosuspend(sdev->dev);
+	if (err < 0)
+		dev_err(sdev->dev, "error: bytes_ext put failed to idle %d\n",
+			err);
+
+	return ret;
+}
+
+int snd_sof_bytes_ext_get(struct snd_kcontrol *kcontrol,
+			  unsigned int __user *binary_data,
+			  unsigned int size)
+{
+	struct soc_bytes_ext *be =
+		(struct soc_bytes_ext *)kcontrol->private_value;
+	struct snd_sof_control *scontrol = be->dobj.private;
+	struct snd_sof_dev *sdev = scontrol->sdev;
+	struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
+	struct snd_ctl_tlv header;
+	struct snd_ctl_tlv __user *tlvd =
+		(struct snd_ctl_tlv __user *)binary_data;
+	int max_size = SOF_IPC_MSG_MAX_SIZE -
+		sizeof(const struct sof_ipc_ctrl_data);
+	int data_size;
+	int err;
+	int ret;
+
+	ret = pm_runtime_get_sync(sdev->dev);
+	if (ret < 0) {
+		dev_err(sdev->dev, "error: bytes_ext get failed to resume %d\n",
+			ret);
+		return ret;
+	}
+
+	/* Decrement the limit by ext bytes header size to ensure
+	 * the user space buffer is not exceeded.
+	 */
+	size -= sizeof(const struct snd_ctl_tlv);
+
+	/* set the ABI header values */
+	cdata->data->magic = SOF_ABI_MAGIC;
+	cdata->data->abi = SOF_ABI_VERSION;
+
+	/* get all the component data from DSP */
+	ret = snd_sof_ipc_get_comp_data(sdev->ipc, scontrol,
+					SOF_IPC_COMP_GET_DATA,
+					SOF_CTRL_TYPE_DATA_GET, scontrol->cmd);
+
+	/* Prevent read of other kernel data or possibly corrupt response */
+	data_size = cdata->data->size + sizeof(const struct sof_abi_hdr);
+	max_size = (size < max_size) ? size : max_size;
+	max_size = (be->max < max_size) ? be->max : max_size;
+	if (data_size > max_size) {
+		dev_err(sdev->dev, "error: user data size %d exceeds max size %d.\n",
+			data_size, max_size);
+		ret = -EINVAL;
+		goto out;
+	}
+
+	header.numid = scontrol->cmd;
+	header.length = data_size;
+	if (copy_to_user(tlvd, &header, sizeof(const struct snd_ctl_tlv))) {
+		ret = -EFAULT;
+		goto out;
+	}
+
+	if (copy_to_user(tlvd->tlv, cdata->data, data_size))
+		return -EFAULT;
+
+out:
+	pm_runtime_mark_last_busy(sdev->dev);
+	err = pm_runtime_put_autosuspend(sdev->dev);
+	if (err < 0)
+		dev_err(sdev->dev, "error: bytes_ext get failed to idle %d\n",
+			err);
+	return ret;
+}
Signature · 1 line
2.17.1
#11 lore raw

Re: [PATCH v3 02/14] ASoC: SOF: Add Sound Open Firmware KControl support

Quoted 16 lines · Pierre-Louis Bossart

From: Liam Girdwood <[email protected]>

SOF exposes regular ALSA Kcontrols that are defined by topology. This patch converts the Kcontrol IO to DSP IPC.

The current implementation is not optimal and can be improved:
a) for every get/put the host wakes up the DSP and generates an IPC. The kernel should cache the values and generate an IPC only when strictly necessary.
b) the firmware can be implemented to only instantiate the pipelines and related control-related parts that are needed at a given time, and power-gate the relevant SRAM blocks.

The development tasks for these two improvements has started, once validated they willl be provided in an update.

Review

Quoted 1 line
(quoted diff) +1 -0
+	max_size = (be->max < max_size) ? be->max : max_size;

min() / max() ?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#12 lore raw

Re: [PATCH v3 02/14] ASoC: SOF: Add Sound Open Firmware KControl support

Quoted 2 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	max_size = (be->max < max_size) ? be->max : max_size;

min() / max() ?

Good catch, this can be simplified.

Not sure I like max_size = min (be->max, max_size), it's confusing.

Maybe

if (be->max < max_size)

    max_size = be->max;

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#13 lore raw

Re: [PATCH v3 02/14] ASoC: SOF: Add Sound Open Firmware KControl support

Quoted 6 lines · Pierre-Louis Bossart
Quoted 2 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	max_size = (be->max < max_size) ? be->max : max_size;

min() / max() ?

Good catch, this can be simplified.

Not sure I like max_size = min (be->max, max_size), it's confusing.

Hmm... It shows intention. For _max size_ you choose _minimum_ value out of two.

Quoted 6 lines

Maybe

if (be->max < max_size)

    max_size = be->max;

It's minor, just choose one style and use it.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#14 lore raw

Re: [PATCH v3 02/14] ASoC: SOF: Add Sound Open Firmware KControl support

In reply to Andy Shevchenko
Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +1 -0
+	max_size = (be->max < max_size) ? be->max : max_size;

min() / max() ?

The use of min() and min3() seems to generate tons of sparse warnings (see below), so I'll leave the code with an explicit test and a comment stating why.

sparse helped us find quite a few issues with endianess, const, iomem qualifiers, I'd like to keep it quiet on non-issues.

  CHECK   sound/soc/sof/control.c sound/soc/sof/control.c:300:20: warning: expression using sizeof(void) sound/soc/sof/control.c:300:20: warning: expression using sizeof(void) sound/soc/sof/control.c:396:20: warning: expression using sizeof(void) sound/soc/sof/control.c:396:20: warning: expression using sizeof(void) sound/soc/sof/control.c:396:20: warning: expression using sizeof(void) sound/soc/sof/control.c:396:20: warning: expression using sizeof(void) sound/soc/sof/control.c:396:20: warning: expression using sizeof(void) sound/soc/sof/control.c:396:20: warning: expression using sizeof(void) sound/soc/sof/control.c:396:20: warning: expression using sizeof(void) sound/soc/sof/control.c:396:20: warning: expression using sizeof(void) sound/soc/sof/control.c:396:20: warning: expression using sizeof(void) sound/soc/sof/control.c:396:20: warning: expression using sizeof(void) sound/soc/sof/control.c:396:20: warning: expression using sizeof(void) sound/soc/sof/control.c:396:20: warning: expression using sizeof(void) sound/soc/sof/control.c:396:20: warning: expression using sizeof(void) sound/soc/sof/control.c:396:20: warning: expression using sizeof(void)

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#15 lore raw

Re: [PATCH v3 02/14] ASoC: SOF: Add Sound Open Firmware KControl support

In reply to Pierre-Louis Bossart
Quoted 4 lines · Pierre-Louis Bossart
sound/soc/sof/control.c new file +3 -0
+int snd_sof_enum_get(struct snd_kcontrol *kcontrol,
+		     struct snd_ctl_elem_value *ucontrol)
+{

....

sound/soc/sof/control.c line 137

Quoted 3 lines
sound/soc/sof/control.c new file +3 -0
+	/* read back each channel */
+	for (i = 0; i < channels; i++)
+		ucontrol->value.integer.value[i] = cdata->chanv[i].value;

enum type needs to access ucontrol->value.enumerated.item[i]. This has a different size, hence using integer.value[] would be broken on BE archs.

Quoted 3 lines
sound/soc/sof/control.c new file +3 -0
+int snd_sof_enum_put(struct snd_kcontrol *kcontrol,
+		     struct snd_ctl_elem_value *ucontrol)
+{

....

sound/soc/sof/control.c line 167

Quoted 3 lines
sound/soc/sof/control.c new file +3 -0
+	/* update each channel */
+	for (i = 0; i < channels; i++)
+		cdata->chanv[i].value = ucontrol->value.integer.value[i];

Ditto.

Quoted 3 lines
sound/soc/sof/control.c new file +3 -0
+int snd_sof_bytes_get(struct snd_kcontrol *kcontrol,
+		      struct snd_ctl_elem_value *ucontrol)
+{

....

sound/soc/sof/control.c line 213

Quoted 10 lines
sound/soc/sof/control.c new file +10 -0
+	size = data->size + sizeof(*data);
+	if (size > be->max) {
+		dev_err(sdev->dev, "error: DSP sent %zu bytes max is %d\n",
+			size, be->max);
+		ret = -EINVAL;
+		goto out;
+	}
+
+	/* copy back to kcontrol */
+	memcpy(ucontrol->value.bytes.data, data, size);

I *hope* that the data size max was already examined not to exceed ucontrol data array size beforehand. But a sanity check to catch a buffer overflow here won't hurt.
Ditto for *_put().

sound/soc/sof/control.c line 276

Quoted 12 lines
sound/soc/sof/control.c new file +12 -0
+int snd_sof_bytes_ext_put(struct snd_kcontrol *kcontrol,
+			  const unsigned int __user *binary_data,
+			  unsigned int size)
+{
+	struct soc_bytes_ext *be =
+		(struct soc_bytes_ext *)kcontrol->private_value;
+	struct snd_sof_control *scontrol = be->dobj.private;
+	struct snd_sof_dev *sdev = scontrol->sdev;
+	struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
+	struct snd_ctl_tlv header;
+	struct snd_ctl_tlv __user *tlvd =
+		(struct snd_ctl_tlv __user *)binary_data;

Don't drop const.

Quoted 19 lines
sound/soc/sof/control.c new file +19 -0
+	int ret;
+	int err;
+	int max_size = SOF_IPC_MSG_MAX_SIZE -
+		sizeof(const struct sof_ipc_ctrl_data);
+
+	ret = pm_runtime_get_sync(sdev->dev);
+	if (ret < 0) {
+		dev_err(sdev->dev, "error: bytes_ext put failed to resume %d\n",
+			ret);
+		return ret;
+	}
+
+	/* The beginning of bytes data contains a header from where
+	 * the length (as bytes) is needed to know the correct copy
+	 * length of data from tlvd->tlv.
+	 */
+	if (copy_from_user(&header, tlvd, sizeof(const struct snd_ctl_tlv))) {
+		ret = -EFAULT;
+		goto out;

sound/soc/sof/control.c line 305

Quoted 10 lines
sound/soc/sof/control.c new file +10 -0
+	}
+	/* The maximum length that can be copied is limited by IPC max
+	 * length and topology defined length for ext bytes control.
+	 */
+	max_size = (be->max < max_size) ? be->max : max_size;
+	if (header.length > max_size) {
+		dev_err(sdev->dev, "error: Bytes data size %d exceeds max %d.\n",
+			header.length, max_size);
+		ret = -EINVAL;
+		goto out;

Here user can pass a malicious data, and printing the error at each time would flood the kernel log. The error message can be dropped or make debug, or use ratelimited version.
Ditto for the rest checks.

thanks,

Takashi

#16 lore raw

Re: [PATCH v3 02/14] ASoC: SOF: Add Sound Open Firmware KControl support

Quoted 6 lines · Takashi Iwai
Quoted 3 lines · Pierre-Louis Bossart
sound/soc/sof/control.c new file +3 -0
+int snd_sof_enum_get(struct snd_kcontrol *kcontrol,
+		     struct snd_ctl_elem_value *ucontrol)
+{

....

Quoted 6 lines

sound/soc/sof/control.c line 137

Quoted 3 lines
sound/soc/sof/control.c new file +3 -0
+	/* read back each channel */
+	for (i = 0; i < channels; i++)
+		ucontrol->value.integer.value[i] = cdata->chanv[i].value;

enum type needs to access ucontrol->value.enumerated.item[i]. This has a different size, hence using integer.value[] would be broken on BE archs.

oops. likely a copy/paste...

Quoted 9 lines
Quoted 4 lines
sound/soc/sof/control.c new file +3 -0
+int snd_sof_enum_put(struct snd_kcontrol *kcontrol,
+		     struct snd_ctl_elem_value *ucontrol)
+{

....

sound/soc/sof/control.c line 167

Quoted 3 lines
sound/soc/sof/control.c new file +3 -0
+	/* update each channel */
+	for (i = 0; i < channels; i++)
+		cdata->chanv[i].value = ucontrol->value.integer.value[i];

Ditto.

same here

Quoted 5 lines
Quoted 4 lines
sound/soc/sof/control.c new file +3 -0
+int snd_sof_bytes_get(struct snd_kcontrol *kcontrol,
+		      struct snd_ctl_elem_value *ucontrol)
+{

....

Quoted 14 lines

sound/soc/sof/control.c line 213

Quoted 10 lines
sound/soc/sof/control.c new file +10 -0
+	size = data->size + sizeof(*data);
+	if (size > be->max) {
+		dev_err(sdev->dev, "error: DSP sent %zu bytes max is %d\n",
+			size, be->max);
+		ret = -EINVAL;
+		goto out;
+	}
+
+	/* copy back to kcontrol */
+	memcpy(ucontrol->value.bytes.data, data, size);

I *hope* that the data size max was already examined not to exceed ucontrol data array size beforehand. But a sanity check to catch a buffer overflow here won't hurt.
Ditto for *_put().

i think we do just that in the 'if' case just above the memcpy, but we'll double-check.

Quoted 14 lines

sound/soc/sof/control.c line 276

Quoted 13 lines
sound/soc/sof/control.c new file +12 -0
+int snd_sof_bytes_ext_put(struct snd_kcontrol *kcontrol,
+			  const unsigned int __user *binary_data,
+			  unsigned int size)
+{
+	struct soc_bytes_ext *be =
+		(struct soc_bytes_ext *)kcontrol->private_value;
+	struct snd_sof_control *scontrol = be->dobj.private;
+	struct snd_sof_dev *sdev = scontrol->sdev;
+	struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
+	struct snd_ctl_tlv header;
+	struct snd_ctl_tlv __user *tlvd =
+		(struct snd_ctl_tlv __user *)binary_data;

Don't drop const.

Ah, I added this cast to make a sparse warning go away, not sure why the const was removed.

I'll double-check again, thanks.

Quoted 26 lines
Quoted 26 lines
sound/soc/sof/control.c new file +25 -0
+	int ret;
+	int err;
+	int max_size = SOF_IPC_MSG_MAX_SIZE -
+		sizeof(const struct sof_ipc_ctrl_data);
+
+	ret = pm_runtime_get_sync(sdev->dev);
+	if (ret < 0) {
+		dev_err(sdev->dev, "error: bytes_ext put failed to resume %d\n",
+			ret);
+		return ret;
+	}
+
+	/* The beginning of bytes data contains a header from where
+	 * the length (as bytes) is needed to know the correct copy
+	 * length of data from tlvd->tlv.
+	 */
+	if (copy_from_user(&header, tlvd, sizeof(const struct snd_ctl_tlv))) {
+		ret = -EFAULT;
+		goto out;
+	}
+	/* The maximum length that can be copied is limited by IPC max
+	 * length and topology defined length for ext bytes control.
+	 */
+	max_size = (be->max < max_size) ? be->max : max_size;
+	if (header.length > max_size) {
Quoted 8 lines

sound/soc/sof/control.c line 305

Quoted 4 lines
sound/soc/sof/control.c new file +4 -0
+		dev_err(sdev->dev, "error: Bytes data size %d exceeds max %d.\n",
+			header.length, max_size);
+		ret = -EINVAL;
+		goto out;

Here user can pass a malicious data, and printing the error at each time would flood the kernel log. The error message can be dropped or make debug, or use ratelimited version.
Ditto for the rest checks.

Good point, we'll fix this.

#17 lore raw

PATCH v3 3/14 ASoC: SOF: Add driver debug support.

In reply to Pierre-Louis Bossart

From: Liam Girdwood <[email protected]>

Add debugFS files that can be used to expose DSP memories and and peripherals to userspace to assist with firmware debugging.

TODO: The runtime_pm parts are not final and will be updated.

---
 sound/soc/sof/debug.c | 176 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 176 insertions(+)
 create mode 100644 sound/soc/sof/debug.c
sound/soc/sof/debug.c new file +176 -0
new file mode 100644
index 000000000000..67f6cf79b729
@@ -0,0 +1,176 @@
+// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
+//
+// This file is provided under a dual BSD/GPLv2 license.  When using or
+// redistributing this file, you may do so under either license.
+//
+// Copyright(c) 2018 Intel Corporation. All rights reserved.
+//
+// Author: Liam Girdwood <[email protected]>
+//
+// Generic debug routines used to export DSP MMIO and memories to userspace
+// for firmware debugging.
+//
+
+#include <linux/debugfs.h>
+#include <linux/io.h>
+#include <linux/pm_runtime.h>
+#include "sof-priv.h"
+
+static int sof_dfsentry_open(struct inode *inode, struct file *file)
+{
+	file->private_data = inode->i_private;
+
+	return 0;
+}
+
+static ssize_t sof_dfsentry_read(struct file *file, char __user *buffer,
+				 size_t count, loff_t *ppos)
+{
+	struct snd_sof_dfsentry_io *dfse = file->private_data;
+	struct snd_sof_dev *sdev = dfse->sdev;
+	int size;
+	u32 *buf;
+	loff_t pos = *ppos;
+	size_t ret;
+
+	size = dfse->size;
+
+	/* validate position & count */
+	if (pos < 0)
+		return -EINVAL;
+	if (pos >= size || !count)
+		return 0;
+	if (count > size - pos)
+		count = size - pos;
+
+	/* intermediate buffer size must be u32 multiple */
+	size = (count + 3) & ~3;
+	buf = kzalloc(size, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
+	/* copy from DSP MMIO */
+	pm_runtime_get_noresume(sdev->dev);
+
+	memcpy_fromio(buf,  dfse->buf + pos, size);
+
+	/*
+	 * TODO: revisit to check if we need mark_last_busy, or if we
+	 * should change to use xxx_put_sync[_suspend]().
+	 */
+	ret = pm_runtime_put_sync_autosuspend(sdev->dev);
+	if (ret < 0)
+		dev_warn(sdev->dev, "warn: debugFS failed to autosuspend %zd\n",
+			 ret);
+
+	/* copy to userspace */
+	ret = copy_to_user(buffer, buf, count);
+	kfree(buf);
+
+	/* update count & position if copy succeeded */
+	if (ret == count)
+		return -EFAULT;
+	count -= ret;
+	*ppos = pos + count;
+
+	return count;
+}
+
+static const struct file_operations sof_dfs_fops = {
+	.open = sof_dfsentry_open,
+	.read = sof_dfsentry_read,
+	.llseek = default_llseek,
+};
+
+/* create FS entry for debug files that can expose DSP memories, registers */
+int snd_sof_debugfs_io_create_item(struct snd_sof_dev *sdev,
+				   void __iomem *base, size_t size,
+				   const char *name)
+{
+	struct snd_sof_dfsentry_io *dfse;
+
+	if (!sdev)
+		return -EINVAL;
+
+	dfse = devm_kzalloc(sdev->dev, sizeof(*dfse), GFP_KERNEL);
+	if (!dfse)
+		return -ENOMEM;
+
+	dfse->buf = base;
+	dfse->size = size;
+	dfse->sdev = sdev;
+
+	dfse->dfsentry = debugfs_create_file(name, 0444, sdev->debugfs_root,
+					     dfse, &sof_dfs_fops);
+	if (!dfse->dfsentry) {
+		dev_err(sdev->dev, "error: cannot create debugfs entry.\n");
+		return -ENODEV;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(snd_sof_debugfs_io_create_item);
+
+/* create FS entry for debug files to expose kernel memory */
+int snd_sof_debugfs_buf_create_item(struct snd_sof_dev *sdev,
+				    void *base, size_t size,
+				    const char *name)
+{
+	struct snd_sof_dfsentry_buf *dfse;
+
+	if (!sdev)
+		return -EINVAL;
+
+	dfse = devm_kzalloc(sdev->dev, sizeof(*dfse), GFP_KERNEL);
+	if (!dfse)
+		return -ENOMEM;
+
+	dfse->buf = base;
+	dfse->size = size;
+	dfse->sdev = sdev;
+
+	dfse->dfsentry = debugfs_create_file(name, 0444, sdev->debugfs_root,
+					     dfse, &sof_dfs_fops);
+	if (!dfse->dfsentry) {
+		dev_err(sdev->dev, "error: cannot create debugfs entry.\n");
+		return -ENODEV;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(snd_sof_debugfs_buf_create_item);
+
+int snd_sof_dbg_init(struct snd_sof_dev *sdev)
+{
+	const struct snd_sof_dsp_ops *ops = sdev->ops;
+	const struct snd_sof_debugfs_map *map;
+	int err = 0, i;
+
+	/* use "sof" as top level debugFS dir */
+	sdev->debugfs_root = debugfs_create_dir("sof", NULL);
+	if (IS_ERR_OR_NULL(sdev->debugfs_root)) {
+		dev_err(sdev->dev, "error: failed to create debugfs directory\n");
+		return -EINVAL;
+	}
+
+	/* create debugFS files for platform specific MMIO/DSP memories */
+	for (i = 0; i < ops->debug_map_count; i++) {
+		map = &ops->debug_map[i];
+
+		err = snd_sof_debugfs_io_create_item(sdev, sdev->bar[map->bar] +
+						  map->offset, map->size,
+						  map->name);
+		if (err < 0)
+			dev_err(sdev->dev, "error: cannot create debugfs for %s\n",
+				map->name);
+	}
+
+	return err;
+}
+EXPORT_SYMBOL(snd_sof_dbg_init);
+
+void snd_sof_free_debug(struct snd_sof_dev *sdev)
+{
+	debugfs_remove_recursive(sdev->debugfs_root);
+}
+EXPORT_SYMBOL(snd_sof_free_debug);
Signature · 1 line
2.17.1
#18 lore raw

Re: [PATCH v3 03/14] ASoC: SOF: Add driver debug support.

sound/soc/sof/debug.c line 44

Quoted 9 lines · Pierre-Louis Bossart

From: Liam Girdwood <[email protected]>

Add debugFS files that can be used to expose DSP memories and and peripherals to userspace to assist with firmware debugging.

TODO: The runtime_pm parts are not final and will be updated.

sound/soc/sof/debug.c new file +2 -0
+	if (count > size - pos)
+		count = size - pos;

max() / min()

sound/soc/sof/debug.c line 47

Quoted 2 lines
sound/soc/sof/debug.c new file +2 -0
+	/* intermediate buffer size must be u32 multiple */
+	size = (count + 3) & ~3;

Hmm, don't we have something like round_up()?

sound/soc/sof/debug.c line 108

Quoted 6 lines
sound/soc/sof/debug.c new file +6 -0
+	dfse->dfsentry = debugfs_create_file(name, 0444, sdev->debugfs_root,
+					     dfse, &sof_dfs_fops);
+	if (!dfse->dfsentry) {
+		dev_err(sdev->dev, "error: cannot create debugfs entry.\n");
+		return -ENODEV;
+	}

We shouldn't rely on debugfs files.
Thus, error checking needs to be removed.

Something like

void ()
{
struct dentry *d;

d = debugfs_...();
if (!d)

	dev_err();
}

sound/soc/sof/debug.c line 108

Quoted 6 lines
sound/soc/sof/debug.c new file +6 -0
+	dfse->dfsentry = debugfs_create_file(name, 0444, sdev->debugfs_root,
+					     dfse, &sof_dfs_fops);
+	if (!dfse->dfsentry) {
+		dev_err(sdev->dev, "error: cannot create debugfs entry.\n");
+		return -ENODEV;
+	}

Ditto.

Quoted 7 lines
sound/soc/sof/debug.c new file +7 -0
+	sdev->debugfs_root = debugfs_create_dir("sof", NULL);
+	if (IS_ERR_OR_NULL(sdev->debugfs_root)) {
+		dev_err(sdev->dev, "error: failed to create debugfs directory\n");
+		return -EINVAL;
+	}
+
+	/* create debugFS files for platform specific MMIO/DSP memories */

sound/soc/sof/debug.c line 165

Quoted 10 lines
sound/soc/sof/debug.c new file +9 -0
+	for (i = 0; i < ops->debug_map_count; i++) {
+		map = &ops->debug_map[i];
+
 
+		err = snd_sof_debugfs_io_create_item(sdev, sdev->bar[map->bar] +
+						  map->offset, map->size,
+						  map->name);
+		if (err < 0)
+			dev_err(sdev->dev, "error: cannot create debugfs for %s\n",
+				map->name);

Ditto (error message dups above).

Quoted 5 lines
sound/soc/sof/debug.c new file +5 -0
+	}
+
+	return err;
+}
+EXPORT_SYMBOL(snd_sof_dbg_init);
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#19 lore raw

Re: [PATCH v3 03/14] ASoC: SOF: Add driver debug support.

Quoted 4 lines

sound/soc/sof/debug.c line 44

Quoted 3 lines
sound/soc/sof/debug.c new file +2 -0
+	if (count > size - pos)
+		count = size - pos;

max() / min()

ok

Quoted 4 lines

sound/soc/sof/debug.c line 47

Quoted 3 lines
sound/soc/sof/debug.c new file +2 -0
+	/* intermediate buffer size must be u32 multiple */
+	size = (count + 3) & ~3;

Hmm, don't we have something like round_up()?

yes, round_up (count, 4) would work.

Quoted 12 lines

sound/soc/sof/debug.c line 108

Quoted 7 lines
sound/soc/sof/debug.c new file +6 -0
+	dfse->dfsentry = debugfs_create_file(name, 0444, sdev->debugfs_root,
+					     dfse, &sof_dfs_fops);
+	if (!dfse->dfsentry) {
+		dev_err(sdev->dev, "error: cannot create debugfs entry.\n");
+		return -ENODEV;
+	}

We shouldn't rely on debugfs files.
Thus, error checking needs to be removed.

Something like

Quoted 8 lines

void ()
{
struct dentry *d;

d = debugfs_...();
if (!d)

	dev_err();
}

You lost me on this one. The main purpose of this return -ENODEV is to avoid adding more entries if one item failed. It's not different from what our estimeed colleagues did in skl-debug.c

Quoted 28 lines

sound/soc/sof/debug.c line 108

Quoted 8 lines
sound/soc/sof/debug.c new file +6 -0
+	dfse->dfsentry = debugfs_create_file(name, 0444, sdev->debugfs_root,
+					     dfse, &sof_dfs_fops);
+	if (!dfse->dfsentry) {
+		dev_err(sdev->dev, "error: cannot create debugfs entry.\n");
+		return -ENODEV;
+	}

Ditto.

Quoted 6 lines
sound/soc/sof/debug.c new file +6 -0
+	sdev->debugfs_root = debugfs_create_dir("sof", NULL);
+	if (IS_ERR_OR_NULL(sdev->debugfs_root)) {
+		dev_err(sdev->dev, "error: failed to create debugfs directory\n");
+		return -EINVAL;
+	}
+

sound/soc/sof/debug.c line 165

Quoted 10 lines
sound/soc/sof/debug.c new file +10 -0
+	/* create debugFS files for platform specific MMIO/DSP memories */
+	for (i = 0; i < ops->debug_map_count; i++) {
+		map = &ops->debug_map[i];
+
+		err = snd_sof_debugfs_io_create_item(sdev, sdev->bar[map->bar] +
+						  map->offset, map->size,
+						  map->name);
+		if (err < 0)
+			dev_err(sdev->dev, "error: cannot create debugfs for %s\n",
+				map->name);

Ditto (error message dups above).

Quoted 3 lines

sound/soc/sof/debug.c line 168

Quoted 3 lines
sound/soc/sof/debug.c new file +3 -0
+	}
+
+	return err;

or maybe that's the issue, we shouldn't return an error on the init proper, but I see nothing wrong in stopping when things go south.

Quoted 2 lines
Quoted 2 lines
(quoted diff) +2 -0
+}
+EXPORT_SYMBOL(snd_sof_dbg_init);
maintainer
#20 lore raw

Re: [PATCH v3 03/14] ASoC: SOF: Add driver debug support.

In reply to Pierre-Louis Bossart

sound/soc/sof/debug.c line 53

Quoted 2 lines · Pierre-Louis Bossart
sound/soc/sof/debug.c new file +2 -0
+	/* copy from DSP MMIO */
+	pm_runtime_get_noresume(sdev->dev);

Why are we doing a _get_noresume() here? It won't actually do anything to the device...

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+	memcpy_fromio(buf,  dfse->buf + pos, size);

Extra space?

sound/soc/sof/debug.c line 64

Quoted 8 lines
sound/soc/sof/debug.c new file +8 -0
+	/*
+	 * TODO: revisit to check if we need mark_last_busy, or if we
+	 * should change to use xxx_put_sync[_suspend]().
+	 */
+	ret = pm_runtime_put_sync_autosuspend(sdev->dev);
+	if (ret < 0)
+		dev_warn(sdev->dev, "warn: debugFS failed to autosuspend %zd\n",
+			 ret);

It rather depends what you're doing... I'm definitely confused as to why you need a _sync operation - if you're doing autosuspend stuff presumably you don't care so much if the device gets powered down immediately, and I can't in general see why that'd be important.

sound/soc/sof/debug.c line 112

Quoted 10 lines
sound/soc/sof/debug.c new file +10 -0
+	dfse->dfsentry = debugfs_create_file(name, 0444, sdev->debugfs_root,
+					     dfse, &sof_dfs_fops);
+	if (!dfse->dfsentry) {
+		dev_err(sdev->dev, "error: cannot create debugfs entry.\n");
+		return -ENODEV;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(snd_sof_debugfs_io_create_item);

debugfs uses EXPORT_SYMBOL_GPL().

#21 lore raw

Re: [PATCH v3 03/14] ASoC: SOF: Add driver debug support.

[combined feedback from Ranjani, Liam and me]

Quoted 4 lines

sound/soc/sof/debug.c line 53

Quoted 2 lines
sound/soc/sof/debug.c new file +2 -0
+	/* copy from DSP MMIO */
+	pm_runtime_get_noresume(sdev->dev);

Why are we doing a _get_noresume() here? It won't actually do anything to the device...

the reason we currently do a get_noresume() is so that we can still read the debugfs entries in case of DSP panic, where a regular resume would not succeed.

That said this part will be reworked based on feedback from Rafael whose suggestion was to get rid of the pm calls while reading debugfs. We can make sure that all the values are flushed out of the dsp before suspending which will make the need to wake it up redundant.

Quoted 4 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+
+	memcpy_fromio(buf,  dfse->buf + pos, size);

Extra space?

yep. we run checkpatch.pl --strict all the time and missed this somehow.

Quoted 13 lines

sound/soc/sof/debug.c line 64

Quoted 9 lines
sound/soc/sof/debug.c new file +8 -0
+	/*
+	 * TODO: revisit to check if we need mark_last_busy, or if we
+	 * should change to use xxx_put_sync[_suspend]().
+	 */
+	ret = pm_runtime_put_sync_autosuspend(sdev->dev);
+	if (ret < 0)
+		dev_warn(sdev->dev, "warn: debugFS failed to autosuspend %zd\n",
+			 ret);

It rather depends what you're doing... I'm definitely confused as to why you need a _sync operation - if you're doing autosuspend stuff presumably you don't care so much if the device gets powered down immediately, and I can't in general see why that'd be important.

yes, as stated above we'll rework all this

Quoted 12 lines

sound/soc/sof/debug.c line 112

Quoted 11 lines
sound/soc/sof/debug.c new file +10 -0
+	dfse->dfsentry = debugfs_create_file(name, 0444, sdev->debugfs_root,
+					     dfse, &sof_dfs_fops);
+	if (!dfse->dfsentry) {
+		dev_err(sdev->dev, "error: cannot create debugfs entry.\n");
+		return -ENODEV;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(snd_sof_debugfs_io_create_item);

debugfs uses EXPORT_SYMBOL_GPL().

We don't have a burning desire to keep this as EXPORT_SYMBOL since debugfs is really Linux specific, so will move all these statements as EXPORT_SYMBOL_GPL.

Thanks Mark for your time, much appreciated. We've already dealt with most of the feedback from Takashi/Andy/Daniel so should have a new version of these patches in 1-2 weeks.

#22 lore raw

PATCH v3 4/14 ASoC: SOF: Add support for IPC IO between DSP and Host

In reply to Pierre-Louis Bossart

From: Liam Girdwood <[email protected]>

Define an IPC ABI for all host <--> DSP communication. This ABI should be transport agnostic. i.e. it should work on MMIO and SPI/I2C style interfaces.

---
 include/sound/sof/control.h   | 125 ++++++
 include/sound/sof/dai-intel.h | 175 ++++++++
 include/sound/sof/dai.h       |  75 ++++
 include/sound/sof/header.h    | 158 +++++++
 include/sound/sof/info.h      | 118 +++++
 include/sound/sof/pm.h        |  48 ++
 include/sound/sof/stream.h    | 149 +++++++
 include/sound/sof/trace.h     |  66 +++
 sound/soc/sof/ipc.c           | 813 ++++++++++++++++++++++++++++++++++
 9 files changed, 1727 insertions(+)
 create mode 100644 include/sound/sof/control.h
 create mode 100644 include/sound/sof/dai-intel.h
 create mode 100644 include/sound/sof/dai.h
 create mode 100644 include/sound/sof/header.h
 create mode 100644 include/sound/sof/info.h
 create mode 100644 include/sound/sof/pm.h
 create mode 100644 include/sound/sof/stream.h
 create mode 100644 include/sound/sof/trace.h
 create mode 100644 sound/soc/sof/ipc.c
include/sound/sof/control.h new file +125 -0
new file mode 100644
index 000000000000..7d7d2eba7d76
@@ -0,0 +1,125 @@
+/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) */
+/*
+ * This file is provided under a dual BSD/GPLv2 license.  When using or
+ * redistributing this file, you may do so under either license.
+ *
+ * Copyright(c) 2018 Intel Corporation. All rights reserved.
+ */
+
+#ifndef __INCLUDE_SOUND_SOF_CONTROL_H__
+#define __INCLUDE_SOUND_SOF_CONTROL_H__
+
+#include <uapi/sound/sof/header.h>
+#include <sound/sof/header.h>
+
+/*
+ * Component Mixers and Controls
+ */
+
+/* channel positions - uses same values as ALSA */
+enum sof_ipc_chmap {
+	SOF_CHMAP_UNKNOWN = 0,
+	SOF_CHMAP_NA,		/**< N/A, silent */
+	SOF_CHMAP_MONO,		/**< mono stream */
+	SOF_CHMAP_FL,		/**< front left */
+	SOF_CHMAP_FR,		/**< front right */
+	SOF_CHMAP_RL,		/**< rear left */
+	SOF_CHMAP_RR,		/**< rear right */
+	SOF_CHMAP_FC,		/**< front centre */
+	SOF_CHMAP_LFE,		/**< LFE */
+	SOF_CHMAP_SL,		/**< side left */
+	SOF_CHMAP_SR,		/**< side right */
+	SOF_CHMAP_RC,		/**< rear centre */
+	SOF_CHMAP_FLC,		/**< front left centre */
+	SOF_CHMAP_FRC,		/**< front right centre */
+	SOF_CHMAP_RLC,		/**< rear left centre */
+	SOF_CHMAP_RRC,		/**< rear right centre */
+	SOF_CHMAP_FLW,		/**< front left wide */
+	SOF_CHMAP_FRW,		/**< front right wide */
+	SOF_CHMAP_FLH,		/**< front left high */
+	SOF_CHMAP_FCH,		/**< front centre high */
+	SOF_CHMAP_FRH,		/**< front right high */
+	SOF_CHMAP_TC,		/**< top centre */
+	SOF_CHMAP_TFL,		/**< top front left */
+	SOF_CHMAP_TFR,		/**< top front right */
+	SOF_CHMAP_TFC,		/**< top front centre */
+	SOF_CHMAP_TRL,		/**< top rear left */
+	SOF_CHMAP_TRR,		/**< top rear right */
+	SOF_CHMAP_TRC,		/**< top rear centre */
+	SOF_CHMAP_TFLC,		/**< top front left centre */
+	SOF_CHMAP_TFRC,		/**< top front right centre */
+	SOF_CHMAP_TSL,		/**< top side left */
+	SOF_CHMAP_TSR,		/**< top side right */
+	SOF_CHMAP_LLFE,		/**< left LFE */
+	SOF_CHMAP_RLFE,		/**< right LFE */
+	SOF_CHMAP_BC,		/**< bottom centre */
+	SOF_CHMAP_BLC,		/**< bottom left centre */
+	SOF_CHMAP_BRC,		/**< bottom right centre */
+	SOF_CHMAP_LAST = SOF_CHMAP_BRC,
+};
+
+/* control data type and direction */
+enum sof_ipc_ctrl_type {
+	/*  per channel data - uses struct sof_ipc_ctrl_value_chan */
+	SOF_CTRL_TYPE_VALUE_CHAN_GET = 0,
+	SOF_CTRL_TYPE_VALUE_CHAN_SET,
+	/* component data - uses struct sof_ipc_ctrl_value_comp */
+	SOF_CTRL_TYPE_VALUE_COMP_GET,
+	SOF_CTRL_TYPE_VALUE_COMP_SET,
+	/* bespoke data - struct struct sof_abi_hdr */
+	SOF_CTRL_TYPE_DATA_GET,
+	SOF_CTRL_TYPE_DATA_SET,
+};
+
+/* control command type */
+enum sof_ipc_ctrl_cmd {
+	SOF_CTRL_CMD_VOLUME = 0, /**< maps to ALSA volume style controls */
+	SOF_CTRL_CMD_ENUM,	/**< maps to ALSA enum style controls */
+	SOF_CTRL_CMD_SWITCH,	/**< maps to ALSA switch style controls */
+	SOF_CTRL_CMD_BINARY,	/**< maps to ALSA binary style controls */
+};
+
+/* generic channel mapped value data */
+struct sof_ipc_ctrl_value_chan {
+	uint32_t channel;	/**< channel map - enum sof_ipc_chmap */
+	uint32_t value;
+} __packed;
+
+/* generic component mapped value data */
+struct sof_ipc_ctrl_value_comp {
+	uint32_t index;	/**< component source/sink/control index in control */
+	union {
+		uint32_t uvalue;
+		int32_t svalue;
+	};
+} __packed;
+
+/* generic control data */
+struct sof_ipc_ctrl_data {
+	struct sof_ipc_reply rhdr;
+	uint32_t comp_id;
+
+	/* control access and data type */
+	uint32_t type;		/**< enum sof_ipc_ctrl_type */
+	uint32_t cmd;		/**< enum sof_ipc_ctrl_cmd */
+	uint32_t index;		/**< control index for comps > 1 control */
+
+	/* control data - can either be appended or DMAed from host */
+	struct sof_ipc_host_buffer buffer;
+	uint32_t num_elems;	/**< in array elems or bytes for data type */
+
+	/* reserved for future use */
+	uint32_t reserved[8];
+
+	/* control data - add new types if needed */
+	union {
+		/* channel values can be used by volume type controls */
+		struct sof_ipc_ctrl_value_chan chanv[0];
+		/* component values used by routing controls like mux, mixer */
+		struct sof_ipc_ctrl_value_comp compv[0];
+		/* data can be used by binary controls */
+		struct sof_abi_hdr data[0];
+	};
+} __packed;
+
+#endif
include/sound/sof/dai-intel.h new file +175 -0

175 lines not on this page · raw message

include/sound/sof/dai.h new file +75 -0

75 lines not on this page · raw message

include/sound/sof/header.h new file +158 -0

158 lines not on this page · raw message

include/sound/sof/info.h new file +118 -0

118 lines not on this page · raw message

include/sound/sof/pm.h new file +48 -0

48 lines not on this page · raw message

include/sound/sof/stream.h new file +149 -0

149 lines not on this page · raw message

include/sound/sof/trace.h new file +66 -0

66 lines not on this page · raw message

sound/soc/sof/ipc.c new file +813 -0

813 lines not on this page · raw message

Signature · 1 line
2.17.1
#23 lore raw

Re: [PATCH v3 04/14] ASoC: SOF: Add support for IPC IO between DSP and Host

Quoted 8 lines · Pierre-Louis Bossart

From: Liam Girdwood <[email protected]>

Define an IPC ABI for all host <--> DSP communication. This ABI should be transport agnostic. i.e. it should work on MMIO and SPI/I2C style interfaces.

+ /* ssc1: TINTE */

include/sound/sof/dai-intel.h new file +1 -0
+#define SOF_DAI_INTEL_SSP_QUIRK_TINTE		(1 << 0)

include/sound/sof/dai-intel.h line 25

Quoted 10 lines
include/sound/sof/dai-intel.h new file +10 -0
+ /* ssc1: PINTE */
+#define SOF_DAI_INTEL_SSP_QUIRK_PINTE		(1 << 1)
+ /* ssc2: SMTATF */
+#define SOF_DAI_INTEL_SSP_QUIRK_SMTATF		(1 << 2)
+ /* ssc2: MMRATF */
+#define SOF_DAI_INTEL_SSP_QUIRK_MMRATF		(1 << 3)
+ /* ssc2: PSPSTWFDFD */
+#define SOF_DAI_INTEL_SSP_QUIRK_PSPSTWFDFD	(1 << 4)
+ /* ssc2: PSPSRWFDFD */
+#define SOF_DAI_INTEL_SSP_QUIRK_PSPSRWFDFD	(1 << 5)

Style mismatch. Looks like a candidate for BIT()

include/sound/sof/dai.h line 44

Quoted 4 lines
include/sound/sof/dai.h new file +4 -0
+#define SOF_DAI_FMT_FORMAT_MASK		0x000f
+#define SOF_DAI_FMT_CLOCK_MASK		0x00f0
+#define SOF_DAI_FMT_INV_MASK		0x0f00
+#define SOF_DAI_FMT_MASTER_MASK		0xf000

GENMASK() ?

Quoted 7 lines
include/sound/sof/stream.h new file +7 -0
+/* flags indicating which time stamps are in sync with each other */
+#define	SOF_TIME_HOST_SYNC	(1 << 0)
+#define	SOF_TIME_DAI_SYNC	(1 << 1)
+#define	SOF_TIME_WALL_SYNC	(1 << 2)
+#define	SOF_TIME_STAMP_SYNC	(1 << 3)
+
+/* flags indicating which time stamps are valid */

include/sound/sof/stream.h line 132

Quoted 10 lines
include/sound/sof/stream.h new file +10 -0
+#define	SOF_TIME_HOST_VALID	(1 << 8)
+#define	SOF_TIME_DAI_VALID	(1 << 9)
+#define	SOF_TIME_WALL_VALID	(1 << 10)
+#define	SOF_TIME_STAMP_VALID	(1 << 11)
+
+/* flags indicating time stamps are 64bit else 3use low 32bit */
+#define	SOF_TIME_HOST_64	(1 << 16)
+#define	SOF_TIME_DAI_64		(1 << 17)
+#define	SOF_TIME_WALL_64	(1 << 18)
+#define	SOF_TIME_STAMP_64	(1 << 19)

BIT() ?

include/sound/sof/trace.h line 45

Quoted 2 lines
include/sound/sof/trace.h new file +2 -0
+#define SOF_IPC_PANIC_MAGIC_MASK		0x0ffff000
+#define SOF_IPC_PANIC_CODE_MASK			0x00000fff

GENMASK() ?

Quoted 1 line

+#define IPC_TIMEOUT_MSECS 300

Simple _MS ?

sound/soc/sof/ipc.c line 316

Quoted 12 lines
sound/soc/sof/ipc.c new file +11 -0
+/* find original TX message from DSP reply */
+static struct snd_sof_ipc_msg *sof_ipc_reply_find_msg(struct snd_sof_ipc *ipc,
+						      u32 header)
+{
+	struct snd_sof_dev *sdev = ipc->sdev;
+	struct snd_sof_ipc_msg *msg;
+
+	header = SOF_IPC_MESSAGE_ID(header);
+
 
+	if (list_empty(&ipc->reply_list))
+		goto err;

Redundant. list_for_each_*() have this check already.

Quoted 10 lines
sound/soc/sof/ipc.c new file +10 -0
+
+	list_for_each_entry(msg, &ipc->reply_list, list) {
+		if (SOF_IPC_MESSAGE_ID(msg->header) == header)
+			return msg;
+	}
+
+err:
+	dev_err(sdev->dev, "error: rx list empty but received 0x%x\n",
+		header);
+	return NULL;

sound/soc/sof/ipc.c

Quoted 10 lines
sound/soc/sof/ipc.c new file +8 -0
+}
 
+int snd_sof_ipc_valid(struct snd_sof_dev *sdev)
+{
+	struct sof_ipc_fw_ready *ready = &sdev->fw_ready;
+	struct sof_ipc_fw_version *v = &ready->version;
+
 
+	dev_info(sdev->dev,
+		 " Firmware info: version %d:%d:%d-%s\n",  v->major, v->minor,

Indentation issues.

Quoted 15 lines
sound/soc/sof/ipc.c new file +15 -0
+		 v->micro, v->tag);
+	dev_info(sdev->dev,
+		 " Firmware: ABI %d:%d:%d Kernel ABI %d:%d:%d\n",
+		 SOF_ABI_VERSION_MAJOR(v->abi_version),
+		 SOF_ABI_VERSION_MINOR(v->abi_version),
+		 SOF_ABI_VERSION_PATCH(v->abi_version),
+		 SOF_ABI_MAJOR, SOF_ABI_MINOR, SOF_ABI_PATCH);
+
+	if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, v->abi_version)) {
+		dev_err(sdev->dev, "error: incompatible FW ABI version\n");
+		return -EINVAL;
+	}
+
+	if (ready->debug.bits.build) {
+		dev_info(sdev->dev,

sound/soc/sof/ipc.c line 752

Quoted 10 lines
sound/soc/sof/ipc.c new file +9 -0
+			 " Firmware debug build %d on %s-%s - options:\n"
+			 "  GDB: %s\n"
+			 "  lock debug: %s\n"
+			 "  lock vdebug: %s\n",
+			 v->build, v->date, v->time,
+			 ready->debug.bits.gdb ? "enabled" : "disabled",
+			 ready->debug.bits.locks ? "enabled" : "disabled",
 
+			 ready->debug.bits.locks_verbose ?
+			 "enabled" : "disabled");

I would leave it on one line (only 3 characters more, but better readability).

			 ready->debug.bits.locks_verbose ? "enabled" : "disabled");

sound/soc/sof/ipc.c line 782

Quoted 11 lines
sound/soc/sof/ipc.c new file +10 -0
+	}
+
+	/* copy the fw_version into debugfs at first boot */
+	memcpy(&sdev->fw_version, v, sizeof(*v));
+
+	return 0;
+}
+EXPORT_SYMBOL(snd_sof_ipc_valid);
 
+	dev_dbg(sdev->dev, "pre-allocate %d IPC messages\n",
+		IPC_EMPTY_LIST_SIZE);

Noise.

sound/soc/sof/ipc.c line 797

Quoted 11 lines
sound/soc/sof/ipc.c new file +11 -0
+
+	/* pre-allocate message data */
+	for (i = 0; i < IPC_EMPTY_LIST_SIZE; i++) {
+		msg->msg_data = devm_kzalloc(sdev->dev, PAGE_SIZE, GFP_KERNEL);
+		if (!msg->msg_data)
+			return NULL;
+
+		msg->reply_data = devm_kzalloc(sdev->dev, PAGE_SIZE,
+					       GFP_KERNEL);
+		if (!msg->reply_data)
+			return NULL;

Can't you just get enough (non-continuous?) pages at once via get_free_pages interface? I didn't know that one, so, consider rather a way to look into.

Quoted 9 lines
sound/soc/sof/ipc.c new file +9 -0
+
+		init_waitqueue_head(&msg->waitq);
+		list_add(&msg->list, &ipc->empty_list);
+		msg++;
+	}
+
+	return ipc;
+}
+EXPORT_SYMBOL(snd_sof_ipc_init);
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#24 lore raw

Re: [PATCH v3 04/14] ASoC: SOF: Add support for IPC IO between DSP and Host

Quoted 19 lines · Andy Shevchenko
Quoted 7 lines · Pierre-Louis Bossart

From: Liam Girdwood <[email protected]>

Define an IPC ABI for all host <--> DSP communication. This ABI should be transport agnostic. i.e. it should work on MMIO and SPI/I2C style interfaces.
+ /* ssc1: TINTE */

include/sound/sof/dai-intel.h new file +1 -0
+#define SOF_DAI_INTEL_SSP_QUIRK_TINTE		(1 << 0)

include/sound/sof/dai-intel.h line 25

Quoted 10 lines
include/sound/sof/dai-intel.h new file +10 -0
+ /* ssc1: PINTE */
+#define SOF_DAI_INTEL_SSP_QUIRK_PINTE		(1 << 1)
+ /* ssc2: SMTATF */
+#define SOF_DAI_INTEL_SSP_QUIRK_SMTATF		(1 << 2)
+ /* ssc2: MMRATF */
+#define SOF_DAI_INTEL_SSP_QUIRK_MMRATF		(1 << 3)
+ /* ssc2: PSPSTWFDFD */
+#define SOF_DAI_INTEL_SSP_QUIRK_PSPSTWFDFD	(1 << 4)
+ /* ssc2: PSPSRWFDFD */
+#define SOF_DAI_INTEL_SSP_QUIRK_PSPSRWFDFD	(1 << 5)

Style mismatch. Looks like a candidate for BIT()

Yes but no. This comes from firmware definitions, so it's easier to leave them alone. Changing the kernel side of the IPC for style reasons just adds more work and chances of divergence.

Quoted 27 lines

include/sound/sof/dai.h line 44

Quoted 5 lines
include/sound/sof/dai.h new file +4 -0
+#define SOF_DAI_FMT_FORMAT_MASK		0x000f
+#define SOF_DAI_FMT_CLOCK_MASK		0x00f0
+#define SOF_DAI_FMT_INV_MASK		0x0f00
+#define SOF_DAI_FMT_MASTER_MASK		0xf000

GENMASK() ?

Quoted 7 lines
include/sound/sof/stream.h new file +7 -0
+/* flags indicating which time stamps are in sync with each other */
+#define	SOF_TIME_HOST_SYNC	(1 << 0)
+#define	SOF_TIME_DAI_SYNC	(1 << 1)
+#define	SOF_TIME_WALL_SYNC	(1 << 2)
+#define	SOF_TIME_STAMP_SYNC	(1 << 3)
+
+/* flags indicating which time stamps are valid */

include/sound/sof/stream.h line 132

Quoted 10 lines
include/sound/sof/stream.h new file +10 -0
+#define	SOF_TIME_HOST_VALID	(1 << 8)
+#define	SOF_TIME_DAI_VALID	(1 << 9)
+#define	SOF_TIME_WALL_VALID	(1 << 10)
+#define	SOF_TIME_STAMP_VALID	(1 << 11)
+
+/* flags indicating time stamps are 64bit else 3use low 32bit */
+#define	SOF_TIME_HOST_64	(1 << 16)
+#define	SOF_TIME_DAI_64		(1 << 17)
+#define	SOF_TIME_WALL_64	(1 << 18)
+#define	SOF_TIME_STAMP_64	(1 << 19)

BIT() ?

Quoted 3 lines

include/sound/sof/trace.h line 45

Quoted 2 lines
include/sound/sof/trace.h new file +2 -0
+#define SOF_IPC_PANIC_MAGIC_MASK		0x0ffff000
+#define SOF_IPC_PANIC_CODE_MASK			0x00000fff

GENMASK() ?

same for all, when the definitions are shared with firmware it's better to keep the values as is.

Quoted 3 lines
Quoted 2 lines

+#define IPC_TIMEOUT_MSECS 300

Simple _MS ?

yes.

Quoted 13 lines

sound/soc/sof/ipc.c line 316

Quoted 12 lines
sound/soc/sof/ipc.c new file +11 -0
+/* find original TX message from DSP reply */
+static struct snd_sof_ipc_msg *sof_ipc_reply_find_msg(struct snd_sof_ipc *ipc,
+						      u32 header)
+{
+	struct snd_sof_dev *sdev = ipc->sdev;
+	struct snd_sof_ipc_msg *msg;
+
+	header = SOF_IPC_MESSAGE_ID(header);
+
+	if (list_empty(&ipc->reply_list))
+		goto err;

Redundant. list_for_each_*() have this check already.

ok, nice catch.

Quoted 12 lines
Quoted 12 lines
sound/soc/sof/ipc.c new file +11 -0
+
+	list_for_each_entry(msg, &ipc->reply_list, list) {
+		if (SOF_IPC_MESSAGE_ID(msg->header) == header)
+			return msg;
+	}
+
+err:
+	dev_err(sdev->dev, "error: rx list empty but received 0x%x\n",
+		header);
+	return NULL;
+}
Quoted 8 lines

sound/soc/sof/ipc.c line 728

Quoted 7 lines
sound/soc/sof/ipc.c new file +7 -0
+int snd_sof_ipc_valid(struct snd_sof_dev *sdev)
+{
+	struct sof_ipc_fw_ready *ready = &sdev->fw_ready;
+	struct sof_ipc_fw_version *v = &ready->version;
+
+	dev_info(sdev->dev,
+		 " Firmware info: version %d:%d:%d-%s\n",  v->major, v->minor,

Indentation issues.

I believe they are intentional, but that's Liam's part.

Quoted 27 lines
Quoted 15 lines
sound/soc/sof/ipc.c new file +14 -0
+		 v->micro, v->tag);
+	dev_info(sdev->dev,
+		 " Firmware: ABI %d:%d:%d Kernel ABI %d:%d:%d\n",
+		 SOF_ABI_VERSION_MAJOR(v->abi_version),
+		 SOF_ABI_VERSION_MINOR(v->abi_version),
+		 SOF_ABI_VERSION_PATCH(v->abi_version),
+		 SOF_ABI_MAJOR, SOF_ABI_MINOR, SOF_ABI_PATCH);
+
+	if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, v->abi_version)) {
+		dev_err(sdev->dev, "error: incompatible FW ABI version\n");
+		return -EINVAL;
+	}
+
+	if (ready->debug.bits.build) {

sound/soc/sof/ipc.c line 752

Quoted 10 lines
sound/soc/sof/ipc.c new file +10 -0
+		dev_info(sdev->dev,
+			 " Firmware debug build %d on %s-%s - options:\n"
+			 "  GDB: %s\n"
+			 "  lock debug: %s\n"
+			 "  lock vdebug: %s\n",
+			 v->build, v->date, v->time,
+			 ready->debug.bits.gdb ? "enabled" : "disabled",
+			 ready->debug.bits.locks ? "enabled" : "disabled",
+			 ready->debug.bits.locks_verbose ?
+			 "enabled" : "disabled");

I would leave it on one line (only 3 characters more, but better readability).

Quoted 1 line
			 ready->debug.bits.locks_verbose ? "enabled" : "disabled");

That must be a result of me asking folks to run checkpatch.pl --strict...

Quoted 12 lines

sound/soc/sof/ipc.c line 782

Quoted 11 lines
sound/soc/sof/ipc.c new file +10 -0
+	}
+
+	/* copy the fw_version into debugfs at first boot */
+	memcpy(&sdev->fw_version, v, sizeof(*v));
+
+	return 0;
+}
+EXPORT_SYMBOL(snd_sof_ipc_valid);
+	dev_dbg(sdev->dev, "pre-allocate %d IPC messages\n",
+		IPC_EMPTY_LIST_SIZE);

Noise.

ok

Quoted 14 lines

sound/soc/sof/ipc.c line 797

Quoted 12 lines
sound/soc/sof/ipc.c new file +11 -0
+
+	/* pre-allocate message data */
+	for (i = 0; i < IPC_EMPTY_LIST_SIZE; i++) {
+		msg->msg_data = devm_kzalloc(sdev->dev, PAGE_SIZE, GFP_KERNEL);
+		if (!msg->msg_data)
+			return NULL;
+
+		msg->reply_data = devm_kzalloc(sdev->dev, PAGE_SIZE,
+					       GFP_KERNEL);
+		if (!msg->reply_data)
+			return NULL;

Can't you just get enough (non-continuous?) pages at once via get_free_pages interface? I didn't know that one, so, consider rather a way to look into.

Dunno either, maybe something for Liam to look at?

Quoted 10 lines
Quoted 10 lines
sound/soc/sof/ipc.c new file +9 -0
+
+		init_waitqueue_head(&msg->waitq);
+		list_add(&msg->list, &ipc->empty_list);
+		msg++;
+	}
+
+	return ipc;
+}
+EXPORT_SYMBOL(snd_sof_ipc_init);
#25 lore raw

Re: [PATCH v3 04/14] ASoC: SOF: Add support for IPC IO between DSP and Host

In reply to Pierre-Louis Bossart
Quoted 111 lines · Pierre-Louis Bossart

From: Liam Girdwood <[email protected]>

Define an IPC ABI for all host <--> DSP communication. This ABI should be transport agnostic. i.e. it should work on MMIO and SPI/I2C style interfaces.

---
 include/sound/sof/control.h   | 125 ++++++
 include/sound/sof/dai-intel.h | 175 ++++++++
 include/sound/sof/dai.h       |  75 ++++
 include/sound/sof/header.h    | 158 +++++++
 include/sound/sof/info.h      | 118 +++++
 include/sound/sof/pm.h        |  48 ++
 include/sound/sof/stream.h    | 149 +++++++
 include/sound/sof/trace.h     |  66 +++
 sound/soc/sof/ipc.c           | 813 ++++++++++++++++++++++++++++++++++
 9 files changed, 1727 insertions(+)
 create mode 100644 include/sound/sof/control.h
 create mode 100644 include/sound/sof/dai-intel.h
 create mode 100644 include/sound/sof/dai.h
 create mode 100644 include/sound/sof/header.h
 create mode 100644 include/sound/sof/info.h
 create mode 100644 include/sound/sof/pm.h
 create mode 100644 include/sound/sof/stream.h
 create mode 100644 include/sound/sof/trace.h
 create mode 100644 sound/soc/sof/ipc.c
include/sound/sof/control.h new file +75 -0
new file mode 100644
index 000000000000..7d7d2eba7d76
@@ -0,0 +1,125 @@
+/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) */
+/*
+ * This file is provided under a dual BSD/GPLv2 license.  When using or
+ * redistributing this file, you may do so under either license.
+ *
+ * Copyright(c) 2018 Intel Corporation. All rights reserved.
+ */
+
+#ifndef __INCLUDE_SOUND_SOF_CONTROL_H__
+#define __INCLUDE_SOUND_SOF_CONTROL_H__
+
+#include <uapi/sound/sof/header.h>
+#include <sound/sof/header.h>
+
+/*
+ * Component Mixers and Controls
+ */
+
+/* channel positions - uses same values as ALSA */
+enum sof_ipc_chmap {
+	SOF_CHMAP_UNKNOWN = 0,
+	SOF_CHMAP_NA,		/**< N/A, silent */
+	SOF_CHMAP_MONO,		/**< mono stream */
+	SOF_CHMAP_FL,		/**< front left */
+	SOF_CHMAP_FR,		/**< front right */
+	SOF_CHMAP_RL,		/**< rear left */
+	SOF_CHMAP_RR,		/**< rear right */
+	SOF_CHMAP_FC,		/**< front centre */
+	SOF_CHMAP_LFE,		/**< LFE */
+	SOF_CHMAP_SL,		/**< side left */
+	SOF_CHMAP_SR,		/**< side right */
+	SOF_CHMAP_RC,		/**< rear centre */
+	SOF_CHMAP_FLC,		/**< front left centre */
+	SOF_CHMAP_FRC,		/**< front right centre */
+	SOF_CHMAP_RLC,		/**< rear left centre */
+	SOF_CHMAP_RRC,		/**< rear right centre */
+	SOF_CHMAP_FLW,		/**< front left wide */
+	SOF_CHMAP_FRW,		/**< front right wide */
+	SOF_CHMAP_FLH,		/**< front left high */
+	SOF_CHMAP_FCH,		/**< front centre high */
+	SOF_CHMAP_FRH,		/**< front right high */
+	SOF_CHMAP_TC,		/**< top centre */
+	SOF_CHMAP_TFL,		/**< top front left */
+	SOF_CHMAP_TFR,		/**< top front right */
+	SOF_CHMAP_TFC,		/**< top front centre */
+	SOF_CHMAP_TRL,		/**< top rear left */
+	SOF_CHMAP_TRR,		/**< top rear right */
+	SOF_CHMAP_TRC,		/**< top rear centre */
+	SOF_CHMAP_TFLC,		/**< top front left centre */
+	SOF_CHMAP_TFRC,		/**< top front right centre */
+	SOF_CHMAP_TSL,		/**< top side left */
+	SOF_CHMAP_TSR,		/**< top side right */
+	SOF_CHMAP_LLFE,		/**< left LFE */
+	SOF_CHMAP_RLFE,		/**< right LFE */
+	SOF_CHMAP_BC,		/**< bottom centre */
+	SOF_CHMAP_BLC,		/**< bottom left centre */
+	SOF_CHMAP_BRC,		/**< bottom right centre */
+	SOF_CHMAP_LAST = SOF_CHMAP_BRC,
+};
+
+/* control data type and direction */
+enum sof_ipc_ctrl_type {
+	/*  per channel data - uses struct sof_ipc_ctrl_value_chan */
+	SOF_CTRL_TYPE_VALUE_CHAN_GET = 0,
+	SOF_CTRL_TYPE_VALUE_CHAN_SET,
+	/* component data - uses struct sof_ipc_ctrl_value_comp */
+	SOF_CTRL_TYPE_VALUE_COMP_GET,
+	SOF_CTRL_TYPE_VALUE_COMP_SET,
+	/* bespoke data - struct struct sof_abi_hdr */
+	SOF_CTRL_TYPE_DATA_GET,
+	SOF_CTRL_TYPE_DATA_SET,
+};
+
+/* control command type */
+enum sof_ipc_ctrl_cmd {

include/sound/sof/control.h line 85

Quoted 10 lines
include/sound/sof/control.h new file +10 -0
+	SOF_CTRL_CMD_VOLUME = 0, /**< maps to ALSA volume style controls */
+	SOF_CTRL_CMD_ENUM,	/**< maps to ALSA enum style controls */
+	SOF_CTRL_CMD_SWITCH,	/**< maps to ALSA switch style controls */
+	SOF_CTRL_CMD_BINARY,	/**< maps to ALSA binary style controls */
+};
+
+/* generic channel mapped value data */
+struct sof_ipc_ctrl_value_chan {
+	uint32_t channel;	/**< channel map - enum sof_ipc_chmap */
+	uint32_t value;

Any reason to avoid s32 and u32?
If this is supposed to be shared with user-space (or user-space may use this as a reference of data struct), we should consider placing in uapi directory, too.

Quoted 4 lines
include/sound/sof/control.h new file +4 -0
+/* wait for IPC message reply */
+static int tx_wait_done(struct snd_sof_ipc *ipc, struct snd_sof_ipc_msg *msg,
+			void *reply_data)
+{

include/sound/sof/control.h line 99

Quoted 10 lines
include/sound/sof/control.h new file +10 -0
+	struct snd_sof_dev *sdev = ipc->sdev;
+	struct sof_ipc_cmd_hdr *hdr = (struct sof_ipc_cmd_hdr *)msg->msg_data;
+	unsigned long flags;
+	int ret;
+
+	/* wait for DSP IPC completion */
+	ret = wait_event_timeout(msg->waitq, msg->ipc_complete,
+				 msecs_to_jiffies(IPC_TIMEOUT_MSECS));
+
+	spin_lock_irqsave(&sdev->ipc_lock, flags);

Since this must be a sleepable context, you can safely use spin_lock_irq() here.

include/sound/sof/control.h line 109

Quoted 10 lines
include/sound/sof/control.h new file +10 -0
+/* send IPC message from host to DSP */
+int sof_ipc_tx_message(struct snd_sof_ipc *ipc, u32 header,
+		       void *msg_data, size_t msg_bytes, void *reply_data,
+		       size_t reply_bytes)
+{
+	struct snd_sof_dev *sdev = ipc->sdev;
+	struct snd_sof_ipc_msg *msg;
+	unsigned long flags;
+
+	spin_lock_irqsave(&sdev->ipc_lock, flags);

Ditto. This one calls tx_wait_done() later.

It's better to define more strictly which one can be called from the spinlocked context and which not.

include/sound/sof/control.h line 115

Quoted 6 lines
include/sound/sof/control.h new file +6 -0
+void snd_sof_ipc_free(struct snd_sof_dev *sdev)
+{
+	cancel_work_sync(&sdev->ipc->tx_kwork);
+	cancel_work_sync(&sdev->ipc->rx_kwork);
+}
+EXPORT_SYMBOL(snd_sof_ipc_free);

Not specific to this function but a general question:
why not EXPORT_SYMBOL_GPL() in general in the whole SOF codes?

thanks,

Takashi

#26 lore raw

Re: [PATCH v3 04/14] ASoC: SOF: Add support for IPC IO between DSP and Host

Quoted 11 lines

include/sound/sof/control.h line 85

Quoted 7 lines
include/sound/sof/control.h +4 -0
 
+/* generic channel mapped value data */
+struct sof_ipc_ctrl_value_chan {
+	uint32_t channel;	/**< channel map - enum sof_ipc_chmap */
+	uint32_t value;

Any reason to avoid s32 and u32?
If this is supposed to be shared with user-space (or user-space may use this as a reference of data struct), we should consider placing in uapi directory, too.

it's intentional

The includes shared with userspace are in include/uapi/sound/sof.

All the files in include/sound/sof, and this one in particular, are more for host-dsp IPC.

In those two cases, uapi and IPC files, we don't use s32 and u32. we could move this directory under include/uapi/sound/sof-ipc if you prefer?

Quoted 31 lines
Quoted 6 lines
include/sound/sof/control.h +4 -0
+/* wait for IPC message reply */
+static int tx_wait_done(struct snd_sof_ipc *ipc, struct snd_sof_ipc_msg *msg,
+			void *reply_data)
+{

include/sound/sof/control.h

Quoted 10 lines
include/sound/sof/control.h +10 -0
+	struct snd_sof_dev *sdev = ipc->sdev;
+	struct sof_ipc_cmd_hdr *hdr = (struct sof_ipc_cmd_hdr *)msg->msg_data;
+	unsigned long flags;
+	int ret;
+
+	/* wait for DSP IPC completion */
+	ret = wait_event_timeout(msg->waitq, msg->ipc_complete,
+				 msecs_to_jiffies(IPC_TIMEOUT_MSECS));
+
+	spin_lock_irqsave(&sdev->ipc_lock, flags);

Since this must be a sleepable context, you can safely use spin_lock_irq() here.

include/sound/sof/control.h

Quoted 10 lines
include/sound/sof/control.h +10 -0
+/* send IPC message from host to DSP */
+int sof_ipc_tx_message(struct snd_sof_ipc *ipc, u32 header,
+		       void *msg_data, size_t msg_bytes, void *reply_data,
+		       size_t reply_bytes)
+{
+	struct snd_sof_dev *sdev = ipc->sdev;
+	struct snd_sof_ipc_msg *msg;
+	unsigned long flags;
+
+	spin_lock_irqsave(&sdev->ipc_lock, flags);

Ditto. This one calls tx_wait_done() later.

Quoted 2 lines

It's better to define more strictly which one can be called from the spinlocked context and which not.

This one is for Keyon and team. I've asked that question multiple times and was told the irqsave was needed. Keyon, can you please chime in?

Quoted 10 lines

include/sound/sof/control.h

Quoted 8 lines
include/sound/sof/control.h +6 -0
+void snd_sof_ipc_free(struct snd_sof_dev *sdev)
+{
+	cancel_work_sync(&sdev->ipc->tx_kwork);
+	cancel_work_sync(&sdev->ipc->rx_kwork);
+}
+EXPORT_SYMBOL(snd_sof_ipc_free);

Not specific to this function but a general question:
why not EXPORT_SYMBOL_GPL() in general in the whole SOF codes?

We use a dual license (copied below)

// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
//
// This file is provided under a dual BSD/GPLv2 license.  When using or // redistributing this file, you may do so under either license.

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#27 lore raw

Re: [PATCH v3 04/14] ASoC: SOF: Add support for IPC IO between DSP and Host

Quoted 15 lines · Pierre-Louis Bossart
Quoted 4 lines
Quoted 4 lines
include/sound/sof/control.h +0 -0
Quoted 8 lines

include/sound/sof/control.h line 85

Quoted 4 lines
include/sound/sof/control.h +4 -0
+/* generic channel mapped value data */
+struct sof_ipc_ctrl_value_chan {
+	uint32_t channel;	/**< channel map - enum sof_ipc_chmap */
+	uint32_t value;

Any reason to avoid s32 and u32?
If this is supposed to be shared with user-space (or user-space may use this as a reference of data struct), we should consider placing in uapi directory, too.

it's intentional

Quoted 8 lines

The includes shared with userspace are in include/uapi/sound/sof.

All the files in include/sound/sof, and this one in particular, are more for host-dsp IPC.

In those two cases, uapi and IPC files, we don't use s32 and u32. we could move this directory under include/uapi/sound/sof-ipc if you prefer?

I don't mind so much but just wondered since it looks as if a part of ABI definition. In anyway, u32/s32 are nicer for kernel codes in general. When I read int32_t or such lengthy form, automatically my alarm chimes as if an alien visiting. But it's also no big deal to keep such types.

BTW, if a file is moved to uapi as a part of ABI, the types should be __u32, not u32.

Quoted 9 lines
Quoted 8 lines

include/sound/sof/control.h

Quoted 6 lines
include/sound/sof/control.h +6 -0
+void snd_sof_ipc_free(struct snd_sof_dev *sdev)
+{
+	cancel_work_sync(&sdev->ipc->tx_kwork);
+	cancel_work_sync(&sdev->ipc->rx_kwork);
+}
+EXPORT_SYMBOL(snd_sof_ipc_free);

Not specific to this function but a general question:
why not EXPORT_SYMBOL_GPL() in general in the whole SOF codes?

Quoted 6 lines

We use a dual license (copied below)

// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
//
// This file is provided under a dual BSD/GPLv2 license.  When using or // redistributing this file, you may do so under either license.

Ah, that can of worms...
I don't know whether it makes so much sense to keep such a dual license, but I know it's a hairy problem enough for keeping my fingers away.

thanks,

Takashi
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#28 lore raw

Re: [PATCH v3 04/14] ASoC: SOF: Add support for IPC IO between DSP and Host

In reply to Pierre-Louis Bossart
Quoted 53 lines · Pierre-Louis Bossart
Quoted 11 lines

include/sound/sof/control.h line 85

Quoted 7 lines
include/sound/sof/control.h +4 -0
 
+/* generic channel mapped value data */
+struct sof_ipc_ctrl_value_chan {
+    uint32_t channel;    /**< channel map - enum sof_ipc_chmap */
+    uint32_t value;

Any reason to avoid s32 and u32?
If this is supposed to be shared with user-space (or user-space may use this as a reference of data struct), we should consider placing in uapi directory, too.

it's intentional

The includes shared with userspace are in include/uapi/sound/sof.

All the files in include/sound/sof, and this one in particular, are more for host-dsp IPC.

In those two cases, uapi and IPC files, we don't use s32 and u32. we could move this directory under include/uapi/sound/sof-ipc if you prefer?

Quoted 33 lines
Quoted 12 lines
include/sound/sof/control.h +2 -0
+/* wait for IPC message reply */
+static int tx_wait_done(struct snd_sof_ipc *ipc, struct 

snd_sof_ipc_msg *msg,
+            void *reply_data)
+{
+    struct snd_sof_dev *sdev = ipc->sdev;
+    struct sof_ipc_cmd_hdr *hdr = (struct sof_ipc_cmd_hdr *)msg->msg_data;
+    unsigned long flags;
+    int ret;

include/sound/sof/control.h

Quoted 6 lines
include/sound/sof/control.h +6 -0
+
+    /* wait for DSP IPC completion */
+    ret = wait_event_timeout(msg->waitq, msg->ipc_complete,
+                 msecs_to_jiffies(IPC_TIMEOUT_MSECS));
+
+    spin_lock_irqsave(&sdev->ipc_lock, flags);

Since this must be a sleepable context, you can safely use spin_lock_irq() here.

include/sound/sof/control.h

Quoted 10 lines
include/sound/sof/control.h +10 -0
+/* send IPC message from host to DSP */
+int sof_ipc_tx_message(struct snd_sof_ipc *ipc, u32 header,
+               void *msg_data, size_t msg_bytes, void *reply_data,
+               size_t reply_bytes)
+{
+    struct snd_sof_dev *sdev = ipc->sdev;
+    struct snd_sof_ipc_msg *msg;
+    unsigned long flags;
+
+    spin_lock_irqsave(&sdev->ipc_lock, flags);

Ditto.  This one calls tx_wait_done() later.

Quoted 4 lines
Quoted 2 lines

It's better to define more strictly which one can be called from the spinlocked context and which not.

This one is for Keyon and team. I've asked that question multiple times and was told the irqsave was needed. Keyon, can you please chime in?

we basically have 3 parts where using this ipc_lock:

1. sof_ipc_tx_message(), get lock, update tx_list, schedule tx_work, put lock, then call tx_wait_done();
2. ipc_tx_next_msg() (tx_work itself), get lock, send message, put lock;
2.5. ack/reply ipc interrupt arrived, mark ipc_complete in handler.
3. tx_wait_done(), wait until ipc_complete(or timeout), then get lock, handle the ack/reply, and put lock at last.

|1 -[--]-|-> 3------(done)-[--]-|
      |             ^
      V             |
      |2-[--]-|     |
              |2.5--|

those []s means holding locks.

So, all those 3 functions can't be called from the spin-locked context as they need to hold the lock inside them.

I admit that we are too conservative that using spin_lock_irqsave/restore() here, as Takashi mentioned, here all 3 functions are actually run in normal thread context, I think we can even run them with interrupt enabled(using spin_lock/unlock() directly).

Thanks,
~Keyon

Quoted 20 lines
Quoted 11 lines

include/sound/sof/control.h

Quoted 9 lines
include/sound/sof/control.h +6 -0
+void snd_sof_ipc_free(struct snd_sof_dev *sdev)
+{
+    cancel_work_sync(&sdev->ipc->tx_kwork);
+    cancel_work_sync(&sdev->ipc->rx_kwork);
+}
+EXPORT_SYMBOL(snd_sof_ipc_free);

Not specific to this function but a general question:
why not EXPORT_SYMBOL_GPL() in general in the whole SOF codes?

We use a dual license (copied below)

// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
//
// This file is provided under a dual BSD/GPLv2 license.  When using or // redistributing this file, you may do so under either license.

Quoted 4 lines

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#29 lore raw

Re: [PATCH v3 04/14] ASoC: SOF: Add support for IPC IO between DSP and Host

Quoted 56 lines · Keyon Jie
Quoted 32 lines
Quoted 32 lines
Quoted 11 lines
(quoted diff) +2 -0
+/* wait for IPC message reply */
+static int tx_wait_done(struct snd_sof_ipc *ipc, struct

snd_sof_ipc_msg *msg,
+            void *reply_data)
+{
+    struct snd_sof_dev *sdev = ipc->sdev;
+    struct sof_ipc_cmd_hdr *hdr = (struct sof_ipc_cmd_hdr *)msg->msg_data;
+    unsigned long flags;
+    int ret;

sound/soc/sof/ipc.c line 207

Quoted 6 lines
sound/soc/sof/ipc.c new file +6 -0
+
+    /* wait for DSP IPC completion */
+    ret = wait_event_timeout(msg->waitq, msg->ipc_complete,
+                 msecs_to_jiffies(IPC_TIMEOUT_MSECS));
+
+    spin_lock_irqsave(&sdev->ipc_lock, flags);

Since this must be a sleepable context, you can safely use spin_lock_irq() here.

sound/soc/sof/ipc.c line 249

Quoted 10 lines
sound/soc/sof/ipc.c new file +10 -0
+/* send IPC message from host to DSP */
+int sof_ipc_tx_message(struct snd_sof_ipc *ipc, u32 header,
+               void *msg_data, size_t msg_bytes, void *reply_data,
+               size_t reply_bytes)
+{
+    struct snd_sof_dev *sdev = ipc->sdev;
+    struct snd_sof_ipc_msg *msg;
+    unsigned long flags;
+
+    spin_lock_irqsave(&sdev->ipc_lock, flags);

Ditto.  This one calls tx_wait_done() later.

Quoted 6 lines
Quoted 2 lines

It's better to define more strictly which one can be called from the spinlocked context and which not.

This one is for Keyon and team. I've asked that question multiple times and was told the irqsave was needed. Keyon, can you please chime in?

we basically have 3 parts where using this ipc_lock:

1. sof_ipc_tx_message(), get lock, update tx_list, schedule tx_work, put lock, then call tx_wait_done();
2. ipc_tx_next_msg() (tx_work itself), get lock, send message, put lock; 2.5. ack/reply ipc interrupt arrived, mark ipc_complete in handler.
3. tx_wait_done(), wait until ipc_complete(or timeout), then get lock, handle the ack/reply, and put lock at last.

|1 -[--]-|-> 3------(done)-[--]-|
      |             ^
      V             |
      |2-[--]-|     |
              |2.5--|

those []s means holding locks.

Quoted 8 lines

So, all those 3 functions can't be called from the spin-locked context as they need to hold the lock inside them.

I admit that we are too conservative that using spin_lock_irqsave/restore() here, as Takashi mentioned, here all 3 functions are actually run in normal thread context, I think we can even run them with interrupt enabled(using spin_lock/unlock() directly).

Well, if we can use spin_lock() variant, mutex is often a better alternative.

The most important point is to know which particular calls may be called in spinlocked / interrupt context beforehand and which are not. This reflects to the API design.

thanks,

Takashi
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#30 lore raw

Re: [PATCH v3 04/14] ASoC: SOF: Add support for IPC IO between DSP and Host

Quoted 67 lines · Takashi Iwai
Quoted 56 lines · Keyon Jie
Quoted 32 lines
Quoted 32 lines
Quoted 11 lines
(quoted diff) +2 -0
+/* wait for IPC message reply */
+static int tx_wait_done(struct snd_sof_ipc *ipc, struct

snd_sof_ipc_msg *msg,
+            void *reply_data)
+{
+    struct snd_sof_dev *sdev = ipc->sdev;
+    struct sof_ipc_cmd_hdr *hdr = (struct sof_ipc_cmd_hdr *)msg->msg_data;
+    unsigned long flags;
+    int ret;

sound/soc/sof/ipc.c line 207

Quoted 6 lines
sound/soc/sof/ipc.c new file +6 -0
+
+    /* wait for DSP IPC completion */
+    ret = wait_event_timeout(msg->waitq, msg->ipc_complete,
+                 msecs_to_jiffies(IPC_TIMEOUT_MSECS));
+
+    spin_lock_irqsave(&sdev->ipc_lock, flags);

Since this must be a sleepable context, you can safely use spin_lock_irq() here.

sound/soc/sof/ipc.c line 249

Quoted 10 lines
sound/soc/sof/ipc.c new file +10 -0
+/* send IPC message from host to DSP */
+int sof_ipc_tx_message(struct snd_sof_ipc *ipc, u32 header,
+               void *msg_data, size_t msg_bytes, void *reply_data,
+               size_t reply_bytes)
+{
+    struct snd_sof_dev *sdev = ipc->sdev;
+    struct snd_sof_ipc_msg *msg;
+    unsigned long flags;
+
+    spin_lock_irqsave(&sdev->ipc_lock, flags);

Ditto.  This one calls tx_wait_done() later.

Quoted 6 lines
Quoted 2 lines

It's better to define more strictly which one can be called from the spinlocked context and which not.

This one is for Keyon and team. I've asked that question multiple times and was told the irqsave was needed. Keyon, can you please chime in?

we basically have 3 parts where using this ipc_lock:

1. sof_ipc_tx_message(), get lock, update tx_list, schedule tx_work, put lock, then call tx_wait_done();
2. ipc_tx_next_msg() (tx_work itself), get lock, send message, put lock; 2.5. ack/reply ipc interrupt arrived, mark ipc_complete in handler.
3. tx_wait_done(), wait until ipc_complete(or timeout), then get lock, handle the ack/reply, and put lock at last.

|1 -[--]-|-> 3------(done)-[--]-|
       |             ^
       V             |
       |2-[--]-|     |
               |2.5--|

those []s means holding locks.

Quoted 8 lines

So, all those 3 functions can't be called from the spin-locked context as they need to hold the lock inside them.

I admit that we are too conservative that using spin_lock_irqsave/restore() here, as Takashi mentioned, here all 3 functions are actually run in normal thread context, I think we can even run them with interrupt enabled(using spin_lock/unlock() directly).

Quoted 6 lines

Well, if we can use spin_lock() variant, mutex is often a better alternative.

The most important point is to know which particular calls may be called in spinlocked / interrupt context beforehand and which are not. This reflects to the API design.

Thanks Takashi, we will refine this part and add comments for each function about these context preconditions.

Thanks,
~Keyon

Quoted 10 lines

thanks,

Takashi
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#31 lore raw

Re: [PATCH v3 04/14] ASoC: SOF: Add support for IPC IO between DSP and Host

In reply to Keyon Jie
Quoted 74 lines · Keyon Jie
Quoted 53 lines · Pierre-Louis Bossart
Quoted 11 lines

include/sound/sof/control.h line 85

Quoted 7 lines
include/sound/sof/control.h +4 -0
 
+/* generic channel mapped value data */
+struct sof_ipc_ctrl_value_chan {
+    uint32_t channel;    /**< channel map - enum sof_ipc_chmap */
+    uint32_t value;

Any reason to avoid s32 and u32?
If this is supposed to be shared with user-space (or user-space may use this as a reference of data struct), we should consider placing in uapi directory, too.

it's intentional

The includes shared with userspace are in include/uapi/sound/sof.

All the files in include/sound/sof, and this one in particular, are more for host-dsp IPC.

In those two cases, uapi and IPC files, we don't use s32 and u32. we could move this directory under include/uapi/sound/sof-ipc if you prefer?

Quoted 33 lines
Quoted 12 lines
include/sound/sof/control.h +2 -0
+/* wait for IPC message reply */
+static int tx_wait_done(struct snd_sof_ipc *ipc, struct 

snd_sof_ipc_msg *msg,
+            void *reply_data)
+{
+    struct snd_sof_dev *sdev = ipc->sdev;
+    struct sof_ipc_cmd_hdr *hdr = (struct sof_ipc_cmd_hdr *)msg->msg_data;
+    unsigned long flags;
+    int ret;

include/sound/sof/control.h

Quoted 6 lines
include/sound/sof/control.h +6 -0
+
+    /* wait for DSP IPC completion */
+    ret = wait_event_timeout(msg->waitq, msg->ipc_complete,
+                 msecs_to_jiffies(IPC_TIMEOUT_MSECS));
+
+    spin_lock_irqsave(&sdev->ipc_lock, flags);

Since this must be a sleepable context, you can safely use spin_lock_irq() here.

include/sound/sof/control.h

Quoted 10 lines
include/sound/sof/control.h +10 -0
+/* send IPC message from host to DSP */
+int sof_ipc_tx_message(struct snd_sof_ipc *ipc, u32 header,
+               void *msg_data, size_t msg_bytes, void *reply_data,
+               size_t reply_bytes)
+{
+    struct snd_sof_dev *sdev = ipc->sdev;
+    struct snd_sof_ipc_msg *msg;
+    unsigned long flags;
+
+    spin_lock_irqsave(&sdev->ipc_lock, flags);

Ditto.  This one calls tx_wait_done() later.

Quoted 4 lines
Quoted 2 lines

It's better to define more strictly which one can be called from the spinlocked context and which not.

This one is for Keyon and team. I've asked that question multiple times and was told the irqsave was needed. Keyon, can you please chime in?

we basically have 3 parts where using this ipc_lock:

1. sof_ipc_tx_message(), get lock, update tx_list, schedule tx_work, put lock, then call tx_wait_done();
2. ipc_tx_next_msg() (tx_work itself), get lock, send message, put lock;
2.5. ack/reply ipc interrupt arrived, mark ipc_complete in handler.
3. tx_wait_done(), wait until ipc_complete(or timeout), then get lock, handle the ack/reply, and put lock at last.

|1 -[--]-|-> 3------(done)-[--]-|
       |             ^
       V             |
       |2-[--]-|     |
               |2.5--|
Quoted 5 lines

those []s means holding locks.

So, all those 3 functions can't be called from the spin-locked context as they need to hold the lock inside them.

I admit that we are too conservative that using spin_lock_irqsave/restore() here, as Takashi mentioned, here all 3 functions are actually run in normal thread context, I think we can even run them with interrupt enabled(using spin_lock/unlock() directly).

Sorry, as #2 is a schedule work where should not sleep, so it is better to use spin_lock/unlock_irq() here.

Thanks,
~Keyon

Quoted 24 lines

Thanks,
~Keyon

Quoted 20 lines
Quoted 11 lines

include/sound/sof/control.h

Quoted 9 lines
include/sound/sof/control.h +6 -0
+void snd_sof_ipc_free(struct snd_sof_dev *sdev)
+{
+    cancel_work_sync(&sdev->ipc->tx_kwork);
+    cancel_work_sync(&sdev->ipc->rx_kwork);
+}
+EXPORT_SYMBOL(snd_sof_ipc_free);

Not specific to this function but a general question:
why not EXPORT_SYMBOL_GPL() in general in the whole SOF codes?

We use a dual license (copied below)

// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
//
// This file is provided under a dual BSD/GPLv2 license.  When using or // redistributing this file, you may do so under either license.

Quoted 8 lines
Quoted 4 lines

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#32 lore raw

Re: [PATCH v3 04/14] ASoC: SOF: Add support for IPC IO between DSP and Host

Quoted 79 lines · Keyon Jie
Quoted 76 lines · Keyon Jie
Quoted 53 lines · Pierre-Louis Bossart
Quoted 11 lines

include/sound/sof/control.h line 85

Quoted 7 lines
include/sound/sof/control.h +4 -0
 
+/* generic channel mapped value data */
+struct sof_ipc_ctrl_value_chan {
+    uint32_t channel;    /**< channel map - enum sof_ipc_chmap */
+    uint32_t value;

Any reason to avoid s32 and u32?
If this is supposed to be shared with user-space (or user-space may use this as a reference of data struct), we should consider placing in uapi directory, too.

it's intentional

The includes shared with userspace are in include/uapi/sound/sof.

All the files in include/sound/sof, and this one in particular, are more for host-dsp IPC.

In those two cases, uapi and IPC files, we don't use s32 and u32. we could move this directory under include/uapi/sound/sof-ipc if you prefer?

Quoted 33 lines
Quoted 12 lines
include/sound/sof/control.h +2 -0
+/* wait for IPC message reply */
+static int tx_wait_done(struct snd_sof_ipc *ipc, struct 

snd_sof_ipc_msg *msg,
+            void *reply_data)
+{
+    struct snd_sof_dev *sdev = ipc->sdev;
+    struct sof_ipc_cmd_hdr *hdr = (struct sof_ipc_cmd_hdr *)msg->msg_data;
+    unsigned long flags;
+    int ret;

include/sound/sof/control.h

Quoted 6 lines
include/sound/sof/control.h +6 -0
+
+    /* wait for DSP IPC completion */
+    ret = wait_event_timeout(msg->waitq, msg->ipc_complete,
+                 msecs_to_jiffies(IPC_TIMEOUT_MSECS));
+
+    spin_lock_irqsave(&sdev->ipc_lock, flags);

Since this must be a sleepable context, you can safely use spin_lock_irq() here.

include/sound/sof/control.h

Quoted 10 lines
include/sound/sof/control.h +10 -0
+/* send IPC message from host to DSP */
+int sof_ipc_tx_message(struct snd_sof_ipc *ipc, u32 header,
+               void *msg_data, size_t msg_bytes, void *reply_data,
+               size_t reply_bytes)
+{
+    struct snd_sof_dev *sdev = ipc->sdev;
+    struct snd_sof_ipc_msg *msg;
+    unsigned long flags;
+
+    spin_lock_irqsave(&sdev->ipc_lock, flags);

Ditto.  This one calls tx_wait_done() later.

Quoted 4 lines
Quoted 2 lines

It's better to define more strictly which one can be called from the spinlocked context and which not.

This one is for Keyon and team. I've asked that question multiple times and was told the irqsave was needed. Keyon, can you please chime in?

we basically have 3 parts where using this ipc_lock:

1. sof_ipc_tx_message(), get lock, update tx_list, schedule tx_work, put lock, then call tx_wait_done();
2. ipc_tx_next_msg() (tx_work itself), get lock, send message, put lock;
2.5. ack/reply ipc interrupt arrived, mark ipc_complete in handler.
3. tx_wait_done(), wait until ipc_complete(or timeout), then get lock, handle the ack/reply, and put lock at last.

|1 -[--]-|-> 3------(done)-[--]-|
       |             ^
       V             |
       |2-[--]-|     |
               |2.5--|

those []s means holding locks.

Quoted 9 lines
Quoted 3 lines

So, all those 3 functions can't be called from the spin-locked context as they need to hold the lock inside them.

I admit that we are too conservative that using spin_lock_irqsave/restore() here, as Takashi mentioned, here all 3 functions are actually run in normal thread context, I think we can even run them with interrupt enabled(using spin_lock/unlock() directly).

Sorry, as #2 is a schedule work where should not sleep, so it is better to use spin_lock/unlock_irq() here.

Thanks,
~Keyon

I just discussed with keyon, got a conclusion that spin_lock_irq should be applied here.

Rander

Quoted 24 lines
Quoted 24 lines

Thanks,
~Keyon

Quoted 20 lines
Quoted 11 lines

include/sound/sof/control.h

Quoted 9 lines
include/sound/sof/control.h +6 -0
+void snd_sof_ipc_free(struct snd_sof_dev *sdev)
+{
+    cancel_work_sync(&sdev->ipc->tx_kwork);
+    cancel_work_sync(&sdev->ipc->rx_kwork);
+}
+EXPORT_SYMBOL(snd_sof_ipc_free);

Not specific to this function but a general question:
why not EXPORT_SYMBOL_GPL() in general in the whole SOF codes?

We use a dual license (copied below)

// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
//
// This file is provided under a dual BSD/GPLv2 license.  When using or // redistributing this file, you may do so under either license.

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

maintainer
#33 lore raw

Re: [PATCH v3 04/14] ASoC: SOF: Add support for IPC IO between DSP and Host

In reply to Pierre-Louis Bossart

sound/soc/sof/ipc.c line 197

Quoted 4 lines · Pierre-Louis Bossart
sound/soc/sof/ipc.c new file +4 -0
+/* wait for IPC message reply */
+static int tx_wait_done(struct snd_sof_ipc *ipc, struct snd_sof_ipc_msg *msg,
+			void *reply_data)
+{

This has exactly one caller, why not inline it (or make both tx and rx separate functions)?

Quoted 7 lines
sound/soc/sof/ipc.c new file +7 -0
+	spin_lock_irqsave(&sdev->ipc_lock, flags);
+
+	/* get an empty message */
+	msg = msg_get_empty(ipc);
+	if (!msg) {
+		spin_unlock_irqrestore(&sdev->ipc_lock, flags);
+		return -EBUSY;

sound/soc/sof/ipc.c line 265

Quoted 10 lines
sound/soc/sof/ipc.c new file +10 -0
+	}
+
+	msg->header = header;
+	msg->msg_size = msg_bytes;
+	msg->reply_size = reply_bytes;
+	msg->ipc_complete = false;
+
+	/* attach any data */
+	if (msg_bytes)
+		memcpy(msg->msg_data, msg_data, msg_bytes);

How big do these messages get? Do we need to hold the lock while we memcpy()?

sound/soc/sof/ipc.c line 272

Quoted 3 lines
sound/soc/sof/ipc.c new file +3 -0
+	/* schedule the message if not busy */
+	if (snd_sof_dsp_is_ready(sdev))
+		schedule_work(&ipc->tx_kwork);

If the DSP is idle is there a reason this has to happen in another thread?

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+	spin_unlock_irqrestore(&sdev->ipc_lock, flags);

The thread is also going to take an irq spinlock after all.

sound/soc/sof/ipc.c line 299

Quoted 4 lines
sound/soc/sof/ipc.c new file +4 -0
+	/* send first message in TX list */
+	msg = list_first_entry(&ipc->tx_list, struct snd_sof_ipc_msg, list);
+	list_move(&msg->list, &ipc->reply_list);
+	snd_sof_dsp_send_msg(sdev, msg);

Should the move happen if the send fails (I see it can return an error code, though we ignore it)?

sound/soc/sof/ipc.c line 408

Quoted 10 lines
sound/soc/sof/ipc.c new file +9 -0
+	int err = -EINVAL;
 
+	case SOF_IPC_FW_READY:
+		/* check for FW boot completion */
+		if (!sdev->boot_complete) {
+			if (sdev->ops->fw_ready)
+				err = sdev->ops->fw_ready(sdev, cmd);
+			if (err < 0) {
+				dev_err(sdev->dev, "error: DSP firmware boot timeout %d\n",
+					err);

err defaults to -EINVAL here which doesn't seem like the right thing if fw_ready() is really optional. Perhaps just validate the ops on registration and call this unconditionally?

sound/soc/sof/ipc.c line 813

Quoted 6 lines
sound/soc/sof/ipc.c new file +6 -0
+void snd_sof_ipc_free(struct snd_sof_dev *sdev)
+{
+	cancel_work_sync(&sdev->ipc->tx_kwork);
+	cancel_work_sync(&sdev->ipc->rx_kwork);
+}
+EXPORT_SYMBOL(snd_sof_ipc_free);

It might be better to have something that stops any new messages being processed as well, to prevent races on removal.

#34 lore raw

Re: [PATCH v3 04/14] ASoC: SOF: Add support for IPC IO between DSP and Host

[consolidated answers from Liam and me]

Quoted 6 lines

sound/soc/sof/ipc.c line 197

Quoted 4 lines
sound/soc/sof/ipc.c new file +4 -0
+/* wait for IPC message reply */
+static int tx_wait_done(struct snd_sof_ipc *ipc, struct snd_sof_ipc_msg *msg,
+			void *reply_data)
+{

This has exactly one caller, why not inline it (or make both tx and rx separate functions)?

we'll look into it.

Quoted 12 lines
Quoted 12 lines
sound/soc/sof/ipc.c new file +11 -0
+	spin_lock_irqsave(&sdev->ipc_lock, flags);
+
+	/* get an empty message */
+	msg = msg_get_empty(ipc);
+	if (!msg) {
+		spin_unlock_irqrestore(&sdev->ipc_lock, flags);
+		return -EBUSY;
+	}
+
+	msg->header = header;
+	msg->msg_size = msg_bytes;
Quoted 8 lines

sound/soc/sof/ipc.c line 265

Quoted 6 lines
sound/soc/sof/ipc.c new file +6 -0
+	msg->reply_size = reply_bytes;
+	msg->ipc_complete = false;
+
+	/* attach any data */
+	if (msg_bytes)
+		memcpy(msg->msg_data, msg_data, msg_bytes);

How big do these messages get? Do we need to hold the lock while we memcpy()?

Messages can be as big as the mailbox, which is hardware dependent. It could be from a few bytes to a larger e.g. 4k page or more, and indeed we need to keep the lock.

Quoted 6 lines

sound/soc/sof/ipc.c line 272

Quoted 4 lines
sound/soc/sof/ipc.c new file +3 -0
+	/* schedule the message if not busy */
+	if (snd_sof_dsp_is_ready(sdev))
+		schedule_work(&ipc->tx_kwork);

If the DSP is idle is there a reason this has to happen in another thread?

we will rename this as snd_sof_dsp_is_ipc_ready() to avoid any confusion with the DSP state. We only care about IPC registers/doorbells at this point, not the fact that the DSP is in its idle loop.

Quoted 4 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+
+	spin_unlock_irqrestore(&sdev->ipc_lock, flags);

The thread is also going to take an irq spinlock after all.

didn't get this point, sorry.

Quoted 7 lines

sound/soc/sof/ipc.c line 299

Quoted 5 lines
sound/soc/sof/ipc.c new file +4 -0
+	/* send first message in TX list */
+	msg = list_first_entry(&ipc->tx_list, struct snd_sof_ipc_msg, list);
+	list_move(&msg->list, &ipc->reply_list);
+	snd_sof_dsp_send_msg(sdev, msg);

Should the move happen if the send fails (I see it can return an error code, though we ignore it)?

so far neither the HDaudio nor the BYT stuff return an error on send_msg, so we'll need to thing about what happens should this happen with other hardware, or demote the status to void.

We'll look into this.

Quoted 13 lines

sound/soc/sof/ipc.c line 408

Quoted 10 lines
sound/soc/sof/ipc.c new file +9 -0
+	int err = -EINVAL;
+	case SOF_IPC_FW_READY:
+		/* check for FW boot completion */
+		if (!sdev->boot_complete) {
+			if (sdev->ops->fw_ready)
+				err = sdev->ops->fw_ready(sdev, cmd);
+			if (err < 0) {
+				dev_err(sdev->dev, "error: DSP firmware boot timeout %d\n",
+					err);

err defaults to -EINVAL here which doesn't seem like the right thing if fw_ready() is really optional. Perhaps just validate the ops on registration and call this unconditionally?

Good catch, err should default to 0, thanks for pointing this out.

Quoted 9 lines

sound/soc/sof/ipc.c line 813

Quoted 7 lines
sound/soc/sof/ipc.c new file +6 -0
+void snd_sof_ipc_free(struct snd_sof_dev *sdev)
+{
+	cancel_work_sync(&sdev->ipc->tx_kwork);
+	cancel_work_sync(&sdev->ipc->rx_kwork);
+}
+EXPORT_SYMBOL(snd_sof_ipc_free);

It might be better to have something that stops any new messages being processed as well, to prevent races on removal.

ok, we will add something to stop the IPC, good suggestion indeed.

maintainer
#35 lore raw

Re: [PATCH v3 04/14] ASoC: SOF: Add support for IPC IO between DSP and Host

Quoted 10 lines · Pierre-Louis Bossart
Quoted 6 lines

sound/soc/sof/ipc.c line 265

Quoted 3 lines
sound/soc/sof/ipc.c new file +3 -0
+	/* attach any data */
+	if (msg_bytes)
+		memcpy(msg->msg_data, msg_data, msg_bytes);

How big do these messages get? Do we need to hold the lock while we memcpy()?

Messages can be as big as the mailbox, which is hardware dependent. It could be from a few bytes to a larger e.g. 4k page or more, and indeed we need to keep the lock.

Is this copying into an actual mailbox or into some data structure in memory? It looked like it's copying into a buffer for sending rather than the mailbox.

Quoted 10 lines
Quoted 6 lines

sound/soc/sof/ipc.c line 272

Quoted 3 lines
sound/soc/sof/ipc.c new file +3 -0
+	/* schedule the message if not busy */
+	if (snd_sof_dsp_is_ready(sdev))
+		schedule_work(&ipc->tx_kwork);

If the DSP is idle is there a reason this has to happen in another thread?

we will rename this as snd_sof_dsp_is_ipc_ready() to avoid any confusion with the DSP state. We only care about IPC registers/doorbells at this point, not the fact that the DSP is in its idle loop.

You're missing the point - why can't we just immediately send the message to the DSP from here, what's the benefit of scheduling some work to do that?

Quoted 5 lines
Quoted 3 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	spin_unlock_irqrestore(&sdev->ipc_lock, flags);

The thread is also going to take an irq spinlock after all.

didn't get this point, sorry.

One reason to bounce to another thread would be to do something that you can't do in this context like take a lock in a place you can't take locks but here we're taking a lock that needs thread context already.

#36 lore raw

Re: [PATCH v3 04/14] ASoC: SOF: Add support for IPC IO between DSP and Host

Quoted 7 lines · Mark Brown
Quoted 5 lines · Pierre-Louis Bossart
Quoted 5 lines

sound/soc/sof/ipc.c line 265

Quoted 3 lines
sound/soc/sof/ipc.c new file +3 -0
+	/* attach any data */
+	if (msg_bytes)
+		memcpy(msg->msg_data, msg_data, msg_bytes);

How big do these messages get? Do we need to hold the lock while we memcpy()?

Quoted 6 lines
Quoted 3 lines

Messages can be as big as the mailbox, which is hardware dependent. It could be from a few bytes to a larger e.g. 4k page or more, and indeed we need to keep the lock.

Is this copying into an actual mailbox or into some data structure in memory? It looked like it's copying into a buffer for sending rather than the mailbox.

I realize between your feedback and Daniel's that we have a terminology issue. On the Intel side we don't have a dedicated hardware mailbox unit, it's only doorbell registers and shared memory windows.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

sound/soc/sof/ipc.c line 272

Quoted 4 lines
sound/soc/sof/ipc.c new file +3 -0
+	/* schedule the message if not busy */
+	if (snd_sof_dsp_is_ready(sdev))
+		schedule_work(&ipc->tx_kwork);

If the DSP is idle is there a reason this has to happen in another thread?

Quoted 6 lines
Quoted 3 lines

we will rename this as snd_sof_dsp_is_ipc_ready() to avoid any confusion with the DSP state. We only care about IPC registers/doorbells at this point, not the fact that the DSP is in its idle loop.

You're missing the point - why can't we just immediately send the message to the DSP from here, what's the benefit of scheduling some work to do that?

I realize this might be Intel-specific and will likely have to evolve.

Keyon/Liam, this is your code, can you comment on Mark's comments (here)

Quoted 7 lines
Quoted 4 lines
Quoted 3 lines

Review

Quoted 2 lines
(quoted diff) +1 -0
+	spin_unlock_irqrestore(&sdev->ipc_lock, flags);

The thread is also going to take an irq spinlock after all.

didn't get this point, sorry.

One reason to bounce to another thread would be to do something that you can't do in this context like take a lock in a place you can't take locks but here we're taking a lock that needs thread context already.

Liam/Keyon comment needed here as well.

I think there are multiple questions that should be better answered to

1. why do we schedule a thread when the DSP is not busy

2. what happens if it is?

3. can we avoid freeing the lock to take it again when we schedule?

maintainer
#37 lore raw

Re: [PATCH v3 04/14] ASoC: SOF: Add support for IPC IO between DSP and Host

Quoted 7 lines · Pierre-Louis Bossart
Quoted 5 lines · Mark Brown
Quoted 3 lines · Pierre-Louis Bossart

Messages can be as big as the mailbox, which is hardware dependent. It could be from a few bytes to a larger e.g. 4k page or more, and indeed we need to keep the lock.

Quoted 7 lines
Quoted 3 lines

Is this copying into an actual mailbox or into some data structure in memory? It looked like it's copying into a buffer for sending rather than the mailbox.

I realize between your feedback and Daniel's that we have a terminology issue. On the Intel side we don't have a dedicated hardware mailbox unit, it's only doorbell registers and shared memory windows.

Ah, that's what I thought was going on. What I was expecting was a sequence of operations along the lines of:

1. Allocate a buffer for the message.
2. Fill in the message.
3. Mark the message as readable by the DSP.

so you only need locking for steps 1 and 3 (and a similar path on return).

#38 lore raw

Re: [Sound-open-firmware] [PATCH v3 04/14] ASoC: SOF: Add support for IPC IO between DSP and Host

In reply to Pierre-Louis Bossart
Quoted 25 lines · Pierre-Louis Bossart
Quoted 5 lines · Mark Brown
Quoted 3 lines · Pierre-Louis Bossart
Quoted 3 lines
Quoted 3 lines

+    /* attach any data */
+    if (msg_bytes)
+        memcpy(msg->msg_data, msg_data, msg_bytes);

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

How big do these messages get?  Do we need to hold the lock while we memcpy()?

Messages can be as big as the mailbox, which is hardware dependent. It could be from a few bytes to a larger e.g. 4k page or more, and indeed we need to keep the lock.

Is this copying into an actual mailbox or into some data structure in memory?  It looked like it's copying into a buffer for sending rather than the mailbox.

I realize between your feedback and Daniel's that we have a terminology issue. On the Intel side we don't have a dedicated hardware mailbox unit, it's only doorbell registers and shared memory windows.

Quoted 9 lines
Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

+    /* schedule the message if not busy */
+    if (snd_sof_dsp_is_ready(sdev))
+        schedule_work(&ipc->tx_kwork);

If the DSP is idle is there a reason this has to happen in another thread?

we will rename this as snd_sof_dsp_is_ipc_ready() to avoid any confusion with the DSP state. We only care about IPC registers/doorbells at this point, not the fact that the DSP is in its idle loop.

Quoted 7 lines
Quoted 3 lines

You're missing the point - why can't we just immediately send the message to the DSP from here, what's the benefit of scheduling some work to do that?

I realize this might be Intel-specific and will likely have to evolve.

Keyon/Liam, this is your code, can you comment on Mark's comments (here)

Hi Mark, I think you are right, we can actually call the message sending immediately here as we already hold the lock which can avoid race condition.

We scheduled another thread to do the sending task, aimed to make sure those messages(required to be sent from different thread) can be sent in sequence one by one(with the FIFO list), but that doesn't means we have to do it in another thread, let's change it.

Quoted 10 lines
Quoted 9 lines
Quoted 6 lines
Quoted 5 lines
Quoted 4 lines

+    spin_unlock_irqrestore(&sdev->ipc_lock, flags);

The thread is also going to take an irq spinlock after all.

didn't get this point, sorry.

One reason to bounce to another thread would be to do something that you can't do in this context like take a lock in a place you can't take locks but here we're taking a lock that needs thread context already.

Quoted 5 lines

Liam/Keyon comment needed here as well.

I think there are multiple questions that should be better answered to

1. why do we schedule a thread when the DSP is not busy

As explain above, I think it's not necessary, let's change it.

Quoted 4 lines

2. what happens if it is?

3. can we avoid freeing the lock to take it again when we schedule?

Then those #2 #3 issues not existed?

Thanks,
~Keyon

Quoted 6 lines

_______________________________________________
Sound-open-firmware mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/sound-open-firmware

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#39 lore raw

Re: [PATCH v3 04/14] ASoC: SOF: Add support for IPC IO between DSP and Host

In reply to Pierre-Louis Bossart

<snip>

sound/soc/sof/ipc.c line 292

Quoted 12 lines
sound/soc/sof/ipc.c new file +12 -0
+/* send next IPC message in list */
+static void ipc_tx_next_msg(struct work_struct *work)
+{
+       struct snd_sof_ipc *ipc =
+               container_of(work, struct snd_sof_ipc, tx_kwork);
+       struct snd_sof_dev *sdev = ipc->sdev;
+       struct snd_sof_ipc_msg *msg;
+       unsigned long flags;
+
+       spin_lock_irqsave(&sdev->ipc_lock, flags);
+
+       /* send message if HW read and message in TX list */

s/HW read/HW ready/ I think.

Quoted 3 lines
sound/soc/sof/ipc.c new file +3 -0
+       if (list_empty(&ipc->tx_list) || !snd_sof_dsp_is_ready(sdev))
+               goto out;
+
#40 lore raw

Re: [PATCH v3 04/14] ASoC: SOF: Add support for IPC IO between DSP and Host

Quoted 13 lines

sound/soc/sof/ipc.c line 292

Quoted 12 lines
sound/soc/sof/ipc.c new file +11 -0
+static void ipc_tx_next_msg(struct work_struct *work)
+{
+       struct snd_sof_ipc *ipc =
+               container_of(work, struct snd_sof_ipc, tx_kwork);
+       struct snd_sof_dev *sdev = ipc->sdev;
+       struct snd_sof_ipc_msg *msg;
+       unsigned long flags;
+
+       spin_lock_irqsave(&sdev->ipc_lock, flags);
+
+       /* send message if HW read and message in TX list */

s/HW read/HW ready/ I think.

yes indeed, thanks for the note.

#41 lore raw

PATCH v3 5/14 ASoC: SOF: Add PCM operations support

In reply to Pierre-Louis Bossart

From: Liam Girdwood <[email protected]>

Add support for exposing PCMs to userspace. PCMs are defined by topology and the operations in this patch map to SOF IPC calls.

---
 sound/soc/sof/pcm.c | 755 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 755 insertions(+)
 create mode 100644 sound/soc/sof/pcm.c
sound/soc/sof/pcm.c new file +755 -0
new file mode 100644
index 000000000000..1b01383a380d
@@ -0,0 +1,755 @@
+// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
+//
+// This file is provided under a dual BSD/GPLv2 license.  When using or
+// redistributing this file, you may do so under either license.
+//
+// Copyright(c) 2018 Intel Corporation. All rights reserved.
+//
+// Author: Liam Girdwood <[email protected]>
+//
+// PCM Layer, interface between ALSA and IPC.
+//
+
+#include <linux/pm_runtime.h>
+#include <sound/pcm_params.h>
+#include <sound/sof.h>
+#include "sof-priv.h"
+#include "ops.h"
+
+#define DRV_NAME	"sof-audio"
+
+/* Create DMA buffer page table for DSP */
+static int create_page_table(struct snd_pcm_substream *substream,
+			     unsigned char *dma_area, size_t size)
+{
+	struct snd_soc_pcm_runtime *rtd = substream->private_data;
+	struct snd_soc_component *component =
+		snd_soc_rtdcom_lookup(rtd, DRV_NAME);
+	struct snd_sof_dev *sdev =
+		snd_soc_component_get_drvdata(component);
+	struct snd_sof_pcm *spcm = rtd->private;
+	struct snd_dma_buffer *dmab = snd_pcm_get_dma_buf(substream);
+	int stream = substream->stream;
+
+	return snd_sof_create_page_table(sdev, dmab,
+		spcm->stream[stream].page_table.area, size);
+}
+
+/* this may get called several times by oss emulation */
+static int sof_pcm_hw_params(struct snd_pcm_substream *substream,
+			     struct snd_pcm_hw_params *params)
+{
+	struct snd_soc_pcm_runtime *rtd = substream->private_data;
+	struct snd_pcm_runtime *runtime = substream->runtime;
+	struct snd_soc_component *component =
+		snd_soc_rtdcom_lookup(rtd, DRV_NAME);
+	struct snd_sof_dev *sdev =
+		snd_soc_component_get_drvdata(component);
+	struct snd_sof_pcm *spcm = rtd->private;
+	struct sof_ipc_pcm_params pcm;
+	struct sof_ipc_pcm_params_reply ipc_params_reply;
+	int posn_offset;
+	int ret;
+
+	/* nothing todo for BE */
+	if (rtd->dai_link->no_pcm)
+		return 0;
+
+	dev_dbg(sdev->dev, "pcm: hw params stream %d dir %d\n",
+		spcm->pcm.pcm_id, substream->stream);
+
+	memset(&pcm, 0, sizeof(pcm));
+
+	/* allocate audio buffer pages */
+	ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
+	if (ret < 0) {
+		dev_err(sdev->dev, "error: could not allocate %d bytes for PCM %d\n",
+			params_buffer_bytes(params), ret);
+		return ret;
+	}
+
+	/* craete compressed page table for audio firmware */
+	ret = create_page_table(substream, runtime->dma_area,
+				runtime->dma_bytes);
+	if (ret < 0)
+		return ret;
+
+	/* number of pages should be rounded up */
+	if (runtime->dma_bytes % PAGE_SIZE)
+		pcm.params.buffer.pages = (runtime->dma_bytes / PAGE_SIZE) + 1;
+	else
+		pcm.params.buffer.pages = runtime->dma_bytes / PAGE_SIZE;
+
+	/* set IPC PCM parameters */
+	pcm.hdr.size = sizeof(pcm);
+	pcm.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS;
+	pcm.comp_id = spcm->stream[substream->stream].comp_id;
+	pcm.params.hdr.size = sizeof(pcm.params);
+	pcm.params.buffer.phy_addr =
+		spcm->stream[substream->stream].page_table.addr;
+	pcm.params.buffer.size = runtime->dma_bytes;
+	pcm.params.buffer.offset = 0;
+	pcm.params.direction = substream->stream;
+	pcm.params.sample_valid_bytes = params_width(params) >> 3;
+	pcm.params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED;
+	pcm.params.rate = params_rate(params);
+	pcm.params.channels = params_channels(params);
+	pcm.params.host_period_bytes = params_period_bytes(params);
+
+	/* container size */
+	switch (params_width(params)) {
+	case 16:
+		pcm.params.sample_container_bytes = 2;
+		break;
+	case 24:
+		pcm.params.sample_container_bytes = 4;
+		break;
+	case 32:
+		pcm.params.sample_container_bytes = 4;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	/* format */
+	switch (params_format(params)) {
+	case SNDRV_PCM_FORMAT_S16:
+		pcm.params.frame_fmt = SOF_IPC_FRAME_S16_LE;
+		break;
+	case SNDRV_PCM_FORMAT_S24:
+		pcm.params.frame_fmt = SOF_IPC_FRAME_S24_4LE;
+		break;
+	case SNDRV_PCM_FORMAT_S32:
+		pcm.params.frame_fmt = SOF_IPC_FRAME_S32_LE;
+		break;
+	case SNDRV_PCM_FORMAT_FLOAT:
+		pcm.params.frame_fmt = SOF_IPC_FRAME_FLOAT;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	/* firmware already configured host stream */
+	ret = snd_sof_pcm_platform_hw_params(sdev,
+					     substream,
+					     params,
+					     &pcm.params);
+	dev_dbg(sdev->dev, "stream_tag %d", pcm.params.stream_tag);
+
+	/* send IPC to the DSP */
+	ret = sof_ipc_tx_message(sdev->ipc, pcm.hdr.cmd, &pcm, sizeof(pcm),
+				 &ipc_params_reply, sizeof(ipc_params_reply));
+
+	/* validate offset */
+	posn_offset = ipc_params_reply.posn_offset;
+
+	/* check if offset is overflow or it is not aligned */
+	if (posn_offset > sdev->stream_box.size ||
+	    posn_offset % sizeof(struct sof_ipc_stream_posn) != 0) {
+		dev_err(sdev->dev, "error: got wrong posn offset 0x%x for PCM %d\n",
+			posn_offset, ret);
+		return ret;
+	}
+	spcm->posn_offset[substream->stream] =
+		sdev->stream_box.offset + posn_offset;
+
+	/* save pcm hw_params */
+	memcpy(&spcm->params[substream->stream], params, sizeof(*params));
+
+	return ret;
+}
+
+static int sof_pcm_hw_free(struct snd_pcm_substream *substream)
+{
+	struct snd_soc_pcm_runtime *rtd = substream->private_data;
+	struct snd_soc_component *component =
+		snd_soc_rtdcom_lookup(rtd, DRV_NAME);
+	struct snd_sof_dev *sdev =
+		snd_soc_component_get_drvdata(component);
+	struct snd_sof_pcm *spcm = rtd->private;
+	struct sof_ipc_stream stream;
+	struct sof_ipc_reply reply;
+	int ret;
+
+	/* nothing todo for BE */
+	if (rtd->dai_link->no_pcm)
+		return 0;
+
+	dev_dbg(sdev->dev, "pcm: free stream %d dir %d\n", spcm->pcm.pcm_id,
+		substream->stream);
+
+	stream.hdr.size = sizeof(stream);
+	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_FREE;
+	stream.comp_id = spcm->stream[substream->stream].comp_id;
+
+	/* send IPC to the DSP */
+	ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
+				 sizeof(stream), &reply, sizeof(reply));
+
+	snd_pcm_lib_free_pages(substream);
+	return ret;
+}
+
+static int sof_restore_hw_params(struct snd_pcm_substream *substream,
+				 struct snd_sof_pcm *spcm,
+				 struct snd_sof_dev *sdev)
+{
+	snd_pcm_uframes_t host = 0;
+	u64 host_posn;
+	int ret = 0;
+
+	/* resume stream */
+	host_posn = spcm->stream[substream->stream].posn.host_posn;
+	host = bytes_to_frames(substream->runtime, host_posn);
+	dev_dbg(sdev->dev,
+		"PCM: resume stream %d dir %d DMA position %lu\n",
+		spcm->pcm.pcm_id, substream->stream, host);
+
+	/* set hw_params */
+	ret = sof_pcm_hw_params(substream,
+				&spcm->params[substream->stream]);
+	if (ret < 0) {
+		dev_err(sdev->dev,
+			"error: set pcm hw_params after resume\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int sof_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
+{
+	struct snd_soc_pcm_runtime *rtd = substream->private_data;
+	struct snd_soc_component *component =
+		snd_soc_rtdcom_lookup(rtd, DRV_NAME);
+	struct snd_sof_dev *sdev =
+		snd_soc_component_get_drvdata(component);
+	struct snd_sof_pcm *spcm = rtd->private;
+	struct sof_ipc_stream stream;
+	struct sof_ipc_reply reply;
+	int ret = 0;
+
+	/* nothing todo for BE */
+	if (rtd->dai_link->no_pcm)
+		return 0;
+
+	dev_dbg(sdev->dev, "pcm: trigger stream %d dir %d cmd %d\n",
+		spcm->pcm.pcm_id, substream->stream, cmd);
+
+	stream.hdr.size = sizeof(stream);
+	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG;
+	stream.comp_id = spcm->stream[substream->stream].comp_id;
+
+	switch (cmd) {
+	case SNDRV_PCM_TRIGGER_STOP:
+
+		/*
+		 * Check if stream was marked for restore before suspend
+		 */
+		if (spcm->restore_stream[substream->stream]) {
+
+			/* unset restore_stream */
+			spcm->restore_stream[substream->stream] = 0;
+
+			/* do not send ipc as the stream hasn't been set up */
+			return 0;
+		}
+
+		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_STOP;
+		break;
+	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
+		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_PAUSE;
+		break;
+	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
+
+		/* check if the stream hw_params needs to be restored */
+		if (spcm->restore_stream[substream->stream]) {
+
+			/* restore hw_params */
+			ret = sof_restore_hw_params(substream, spcm, sdev);
+			if (ret < 0)
+				return ret;
+
+			/* unset restore_stream */
+			spcm->restore_stream[substream->stream] = 0;
+
+			/* trigger start */
+			stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_START;
+		} else {
+
+			/* trigger pause release */
+			stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_RELEASE;
+		}
+		break;
+	case SNDRV_PCM_TRIGGER_START:
+		/* fall through */
+	case SNDRV_PCM_TRIGGER_RESUME:
+
+		/* check if the stream hw_params needs to be restored */
+		if (spcm->restore_stream[substream->stream]) {
+
+			/* restore hw_params */
+			ret = sof_restore_hw_params(substream, spcm, sdev);
+			if (ret < 0)
+				return ret;
+
+			/* unset restore_stream */
+			spcm->restore_stream[substream->stream] = 0;
+		}
+
+		/* trigger stream */
+		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_START;
+
+		break;
+	case SNDRV_PCM_TRIGGER_SUSPEND:
+		break;
+	default:
+		dev_err(sdev->dev, "error: unhandled trigger cmd %d\n", cmd);
+		return -EINVAL;
+	}
+
+	snd_sof_pcm_platform_trigger(sdev, substream, cmd);
+
+	/* send IPC to the DSP */
+	ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
+				 sizeof(stream), &reply, sizeof(reply));
+
+	return ret;
+}
+
+static snd_pcm_uframes_t sof_pcm_pointer(struct snd_pcm_substream *substream)
+{
+	struct snd_soc_pcm_runtime *rtd = substream->private_data;
+	struct snd_soc_component *component =
+		snd_soc_rtdcom_lookup(rtd, DRV_NAME);
+	struct snd_sof_dev *sdev =
+		snd_soc_component_get_drvdata(component);
+	struct snd_sof_pcm *spcm = rtd->private;
+	snd_pcm_uframes_t host = 0, dai = 0;
+
+	/* nothing todo for BE */
+	if (rtd->dai_link->no_pcm)
+		return 0;
+
+	/* if have dsp ops pointer callback, use that directly */
+	if (sdev->hda && sdev->ops->pcm_pointer)
+		return sdev->ops->pcm_pointer(sdev, substream);
+
+	/* read position from DSP */
+	host = bytes_to_frames(substream->runtime,
+			       spcm->stream[substream->stream].posn.host_posn);
+	dai = bytes_to_frames(substream->runtime,
+			      spcm->stream[substream->stream].posn.dai_posn);
+
+	dev_dbg(sdev->dev, "PCM: stream %d dir %d DMA position %lu DAI position %lu\n",
+		spcm->pcm.pcm_id, substream->stream, host, dai);
+
+	return host;
+}
+
+static int sof_pcm_open(struct snd_pcm_substream *substream)
+{
+	struct snd_soc_pcm_runtime *rtd = substream->private_data;
+	struct snd_pcm_runtime *runtime = substream->runtime;
+	struct snd_soc_component *component =
+		snd_soc_rtdcom_lookup(rtd, DRV_NAME);
+	struct snd_sof_dev *sdev =
+		snd_soc_component_get_drvdata(component);
+	struct snd_sof_pcm *spcm = rtd->private;
+	struct snd_soc_tplg_stream_caps *caps =
+		&spcm->pcm.caps[substream->stream];
+	int ret;
+
+	/* nothing todo for BE */
+	if (rtd->dai_link->no_pcm)
+		return 0;
+
+	dev_dbg(sdev->dev, "pcm: open stream %d dir %d\n", spcm->pcm.pcm_id,
+		substream->stream);
+
+	mutex_lock(&spcm->mutex);
+
+	ret = pm_runtime_get_sync(sdev->dev);
+	if (ret < 0) {
+		dev_err(sdev->dev, "error: pcm open failed to resume %d\n",
+			ret);
+		mutex_unlock(&spcm->mutex);
+		return ret;
+	}
+
+	/* set any runtime constraints based on topology */
+	snd_pcm_hw_constraint_step(substream->runtime, 0,
+				   SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
+				   le32_to_cpu(caps->period_size_min));
+	snd_pcm_hw_constraint_step(substream->runtime, 0,
+				   SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
+				   le32_to_cpu(caps->period_size_min));
+
+	/* set runtime config */
+	runtime->hw.info = SNDRV_PCM_INFO_MMAP |
+			  SNDRV_PCM_INFO_MMAP_VALID |
+			  SNDRV_PCM_INFO_INTERLEAVED |
+			  SNDRV_PCM_INFO_PAUSE |
+			  SNDRV_PCM_INFO_RESUME |
+			  SNDRV_PCM_INFO_NO_PERIOD_WAKEUP;
+	runtime->hw.formats = le64_to_cpu(caps->formats);
+	runtime->hw.period_bytes_min = le32_to_cpu(caps->period_size_min);
+	runtime->hw.period_bytes_max = le32_to_cpu(caps->period_size_max);
+	runtime->hw.periods_min = le32_to_cpu(caps->periods_min);
+	runtime->hw.periods_max = le32_to_cpu(caps->periods_max);
+	runtime->hw.buffer_bytes_max = le32_to_cpu(caps->buffer_size_max);
+
+	dev_dbg(sdev->dev, "period min %zd max %zd bytes\n",
+		runtime->hw.period_bytes_min,
+		runtime->hw.period_bytes_max);
+	dev_dbg(sdev->dev, "period count %d max %d\n",
+		runtime->hw.periods_min,
+		runtime->hw.periods_max);
+	dev_dbg(sdev->dev, "buffer max %zd bytes\n",
+		runtime->hw.buffer_bytes_max);
+
+	/* set wait time - TODO: come from topology */
+	substream->wait_time = 500;
+
+	spcm->stream[substream->stream].posn.host_posn = 0;
+	spcm->stream[substream->stream].posn.dai_posn = 0;
+	spcm->stream[substream->stream].substream = substream;
+
+	snd_sof_pcm_platform_open(sdev, substream);
+
+	mutex_unlock(&spcm->mutex);
+	return 0;
+}
+
+static int sof_pcm_close(struct snd_pcm_substream *substream)
+{
+	struct snd_soc_pcm_runtime *rtd = substream->private_data;
+	struct snd_soc_component *component =
+		snd_soc_rtdcom_lookup(rtd, DRV_NAME);
+	struct snd_sof_dev *sdev =
+		snd_soc_component_get_drvdata(component);
+	struct snd_sof_pcm *spcm = rtd->private;
+	int err;
+
+	/* nothing todo for BE */
+	if (rtd->dai_link->no_pcm)
+		return 0;
+
+	dev_dbg(sdev->dev, "pcm: close stream %d dir %d\n", spcm->pcm.pcm_id,
+		substream->stream);
+
+	snd_sof_pcm_platform_close(sdev, substream);
+
+	mutex_lock(&spcm->mutex);
+	pm_runtime_mark_last_busy(sdev->dev);
+
+	err = pm_runtime_put_autosuspend(sdev->dev);
+	if (err < 0)
+		dev_err(sdev->dev, "error: pcm close failed to idle %d\n",
+			err);
+
+	mutex_unlock(&spcm->mutex);
+	return 0;
+}
+
+static struct snd_pcm_ops sof_pcm_ops = {
+	.open		= sof_pcm_open,
+	.close		= sof_pcm_close,
+	.ioctl		= snd_pcm_lib_ioctl,
+	.hw_params	= sof_pcm_hw_params,
+	.hw_free	= sof_pcm_hw_free,
+	.trigger	= sof_pcm_trigger,
+	.pointer	= sof_pcm_pointer,
+	.page		= snd_pcm_sgbuf_ops_page,
+};
+
+static int sof_pcm_new(struct snd_soc_pcm_runtime *rtd)
+{
+	struct snd_soc_component *component =
+		snd_soc_rtdcom_lookup(rtd, DRV_NAME);
+	struct snd_sof_dev *sdev =
+		snd_soc_component_get_drvdata(component);
+	struct snd_sof_pcm *spcm;
+	struct snd_pcm *pcm = rtd->pcm;
+	struct snd_soc_tplg_stream_caps *caps;
+	int ret = 0, stream = SNDRV_PCM_STREAM_PLAYBACK;
+
+	/* find SOF PCM for this RTD */
+	spcm = snd_sof_find_spcm_dai(sdev, rtd);
+	if (!spcm) {
+		dev_warn(sdev->dev, "warn: can't find PCM with DAI ID %d\n",
+			 rtd->dai_link->id);
+		return 0;
+	}
+	rtd->private = spcm;
+
+	dev_dbg(sdev->dev, "creating new PCM %s\n", spcm->pcm.pcm_name);
+
+	/* do we need to allocate playback PCM DMA pages */
+	if (!spcm->pcm.playback)
+		goto capture;
+
+	caps = &spcm->pcm.caps[stream];
+
+	/* pre-allocate playback audio buffer pages */
+	dev_dbg(sdev->dev, "spcm: allocate %s playback DMA buffer size 0x%x max 0x%x\n",
+		caps->name, caps->buffer_size_min, caps->buffer_size_max);
+
+	ret = snd_pcm_lib_preallocate_pages(pcm->streams[stream].substream,
+					    SNDRV_DMA_TYPE_DEV_SG, sdev->parent,
+					    le32_to_cpu(caps->buffer_size_min),
+					    le32_to_cpu(caps->buffer_size_max));
+	if (ret) {
+		dev_err(sdev->dev, "error: can't alloc DMA buffer size 0x%x/0x%x for %s %d\n",
+			caps->buffer_size_min, caps->buffer_size_max,
+			caps->name, ret);
+		return ret;
+	}
+
+	/* allocate playback page table buffer */
+	ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->parent,
+				  PAGE_SIZE, &spcm->stream[stream].page_table);
+	if (ret < 0) {
+		dev_err(sdev->dev, "error: can't alloc page table for %s %d\n",
+			caps->name, ret);
+		goto free_playback_dma_buffer;
+	}
+
+capture:
+	stream = SNDRV_PCM_STREAM_CAPTURE;
+
+	/* do we need to allocate capture PCM DMA pages */
+	if (!spcm->pcm.capture)
+		return ret;
+
+	caps = &spcm->pcm.caps[stream];
+
+	/* pre-allocate capture audio buffer pages */
+	dev_dbg(sdev->dev, "spcm: allocate %s capture DMA buffer size 0x%x max 0x%x\n",
+		caps->name, caps->buffer_size_min, caps->buffer_size_max);
+
+	ret = snd_pcm_lib_preallocate_pages(pcm->streams[stream].substream,
+					    SNDRV_DMA_TYPE_DEV_SG, sdev->parent,
+					    le32_to_cpu(caps->buffer_size_min),
+					    le32_to_cpu(caps->buffer_size_max));
+	if (ret) {
+		dev_err(sdev->dev, "error: can't alloc DMA buffer size 0x%x/0x%x for %s %d\n",
+			caps->buffer_size_min, caps->buffer_size_max,
+			caps->name, ret);
+		goto free_playback_tables;
+	}
+
+	/* allocate capture page table buffer */
+	ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->parent,
+				  PAGE_SIZE, &spcm->stream[stream].page_table);
+	if (ret < 0) {
+		dev_err(sdev->dev, "error: can't alloc page table for %s %d\n",
+			caps->name, ret);
+		goto free_playback_tables;
+	}
+
+	/* TODO: assign channel maps from topology */
+
+	return ret;
+
+free_playback_tables:
+	if (spcm->pcm.playback)
+		snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table);
+
+free_playback_dma_buffer:
+	/*
+	 * no need to explicitly release preallocated memory,
+	 * snd_pcm_lib_preallocate_free_for_all() is called by the core
+	 */
+
+	return ret;
+}
+
+static void sof_pcm_free(struct snd_pcm *pcm)
+{
+	struct snd_sof_pcm *spcm;
+	struct snd_soc_pcm_runtime *rtd = pcm->private_data;
+	struct snd_soc_component *component =
+		snd_soc_rtdcom_lookup(rtd, DRV_NAME);
+	struct snd_sof_dev *sdev =
+		snd_soc_component_get_drvdata(component);
+
+	spcm = snd_sof_find_spcm_dai(sdev, rtd);
+	if (!spcm) {
+		dev_warn(sdev->dev, "warn: can't find PCM with DAI ID %d\n",
+			 rtd->dai_link->id);
+		return;
+	}
+
+	if (spcm->pcm.playback)
+		snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].page_table);
+
+	if (spcm->pcm.capture)
+		snd_dma_free_pages(&spcm->stream[SNDRV_PCM_STREAM_CAPTURE].page_table);
+
+	/*
+	 * no need to explicitly release preallocated memory,
+	 * snd_pcm_lib_preallocate_free_for_all() is called by the core
+	 */
+}
+
+/* fixup the BE DAI link to match any values from topology */
+static int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd,
+				  struct snd_pcm_hw_params *params)
+{
+	struct snd_interval *rate = hw_param_interval(params,
+			SNDRV_PCM_HW_PARAM_RATE);
+	struct snd_interval *channels = hw_param_interval(params,
+						SNDRV_PCM_HW_PARAM_CHANNELS);
+	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
+	struct snd_soc_component *component =
+		snd_soc_rtdcom_lookup(rtd, DRV_NAME);
+	struct snd_sof_dev *sdev =
+		snd_soc_component_get_drvdata(component);
+	struct snd_sof_dai *dai =
+		snd_sof_find_dai(sdev, (char *)rtd->dai_link->name);
+
+	/* no topology exists for this BE, try a common configuration */
+	if (!dai) {
+		dev_warn(sdev->dev, "warning: no topology found for BE DAI %s config\n",
+			rtd->dai_link->name);
+
+		/*  set 48k, stereo, 16bits by default */
+		rate->min = 48000;
+		rate->max = 48000;
+
+		channels->min = 2;
+		channels->max = 2;
+
+		snd_mask_none(fmt);
+		snd_mask_set(fmt, (__force int)SNDRV_PCM_FORMAT_S16_LE);
+
+		return 0;
+	}
+
+	/* read format from topology */
+	snd_mask_none(fmt);
+
+	switch (dai->comp_dai.config.frame_fmt) {
+	case SOF_IPC_FRAME_S16_LE:
+		snd_mask_set(fmt, (__force int)SNDRV_PCM_FORMAT_S16_LE);
+		break;
+	case SOF_IPC_FRAME_S24_4LE:
+		snd_mask_set(fmt, (__force int)SNDRV_PCM_FORMAT_S24_LE);
+		break;
+	case SOF_IPC_FRAME_S32_LE:
+		snd_mask_set(fmt, (__force int)SNDRV_PCM_FORMAT_S32_LE);
+		break;
+	default:
+		dev_err(sdev->dev, "error: No available DAI format!\n");
+		return -EINVAL;
+	}
+
+	/* read rate and channels from topology */
+	switch (dai->dai_config->type) {
+	case SOF_DAI_INTEL_SSP:
+		rate->min = dai->dai_config->ssp.fsync_rate;
+		rate->max = dai->dai_config->ssp.fsync_rate;
+		channels->min = dai->dai_config->ssp.tdm_slots;
+		channels->max = dai->dai_config->ssp.tdm_slots;
+
+		dev_dbg(sdev->dev,
+			"rate_min: %d rate_max: %d\n", rate->min, rate->max);
+		dev_dbg(sdev->dev,
+			"channels_min: %d channels_max: %d\n",
+			channels->min, channels->max);
+
+		break;
+	case SOF_DAI_INTEL_DMIC:
+		/* DMIC only supports 16 or 32 bit formats */
+		if (dai->comp_dai.config.frame_fmt == SOF_IPC_FRAME_S24_4LE) {
+			dev_err(sdev->dev,
+				"error: invalid fmt %d for DAI type %d\n",
+				dai->comp_dai.config.frame_fmt,
+				dai->dai_config->type);
+		}
+		/* TODO: add any other DMIC specific fixups */
+		break;
+	case SOF_DAI_INTEL_HDA:
+		/* do nothing for HDA dai_link */
+		break;
+	default:
+		dev_err(sdev->dev, "error: invalid DAI type %d\n",
+			dai->dai_config->type);
+		break;
+	}
+
+	return 0;
+}
+
+static int sof_pcm_probe(struct snd_soc_component *component)
+{
+	struct snd_sof_dev *sdev =
+		snd_soc_component_get_drvdata(component);
+	struct snd_sof_pdata *plat_data = dev_get_platdata(component->dev);
+	const char *tplg_filename;
+	int ret, err;
+
+	/* load the default topology */
+	sdev->component = component;
+	tplg_filename = plat_data->machine->sof_tplg_filename;
+
+	ret = snd_sof_load_topology(sdev, tplg_filename);
+	if (ret < 0) {
+		dev_err(sdev->dev, "error: failed to load DSP topology %d\n",
+			ret);
+		goto err;
+	}
+
+	/* enable runtime PM with auto suspend */
+	pm_runtime_set_autosuspend_delay(component->dev,
+					 SND_SOF_SUSPEND_DELAY);
+	pm_runtime_use_autosuspend(component->dev);
+	pm_runtime_enable(component->dev);
+
+	pm_runtime_mark_last_busy(component->dev);
+	err = pm_runtime_put_autosuspend(component->dev);
+	if (err < 0)
+		dev_err(sdev->dev, "error: failed to enter PM idle %d\n", err);
+
+err:
+	return ret;
+}
+
+static void sof_pcm_remove(struct snd_soc_component *component)
+{
+	struct snd_sof_dev *sdev =
+		snd_soc_component_get_drvdata(component);
+
+	pm_runtime_disable(component->dev);
+	snd_sof_free_topology(sdev);
+
+}
+
+void snd_sof_new_platform_drv(struct snd_sof_dev *sdev)
+{
+	struct snd_soc_component_driver *pd = &sdev->plat_drv;
+	struct snd_sof_pdata *plat_data = sdev->pdata;
+	const char *plat_name, *drv_name;
+
+	plat_name = plat_data->machine->asoc_plat_name;
+	drv_name = plat_data->machine->drv_name;
+
+	dev_dbg(sdev->dev, "using platform alias %s\n", plat_name);
+
+	pd->name = "sof-audio";
+	pd->probe = sof_pcm_probe;
+	pd->remove = sof_pcm_remove;
+	pd->ops	= &sof_pcm_ops;
+#if IS_ENABLED(CONFIG_SND_SOC_SOF_COMPRESS)
+	pd->compr_ops = &sof_compressed_ops;
+#endif
+	pd->pcm_new = sof_pcm_new;
+	pd->pcm_free = sof_pcm_free;
+	pd->ignore_machine = drv_name;
+	pd->be_hw_params_fixup = sof_pcm_dai_link_fixup;
+	pd->be_pcm_base = SOF_BE_PCM_BASE;
+	pd->use_dai_pcm_id = true;
+	pd->topology_name_prefix = "sof";
+}
+
Signature · 1 line
2.17.1
#42 lore raw

Re: [PATCH v3 05/14] ASoC: SOF: Add PCM operations support

sound/soc/sof/pcm.c line 81

Quoted 6 lines · Pierre-Louis Bossart
sound/soc/sof/pcm.c new file +5 -0
+	/* number of pages should be rounded up */
+	if (runtime->dma_bytes % PAGE_SIZE)
+		pcm.params.buffer.pages = (runtime->dma_bytes / PAGE_SIZE) + 1;
+	else
+		pcm.params.buffer.pages = runtime->dma_bytes / PAGE_SIZE;

There is likely some nice macro for this :)

sound/soc/sof/pcm.c line 137

Quoted 6 lines
sound/soc/sof/pcm.c new file +6 -0
+	/* firmware already configured host stream */
+	ret = snd_sof_pcm_platform_hw_params(sdev,
+					     substream,
+					     params,
+					     &pcm.params);
+	dev_dbg(sdev->dev, "stream_tag %d", pcm.params.stream_tag);

This error can be ignored?

sound/soc/sof/pcm.c line 141

Quoted 3 lines
sound/soc/sof/pcm.c new file +3 -0
+	/* send IPC to the DSP */
+	ret = sof_ipc_tx_message(sdev->ipc, pcm.hdr.cmd, &pcm, sizeof(pcm),
+				 &ipc_params_reply, sizeof(ipc_params_reply));

This error value isn't evaluated immediately but passed until the last. Is it intentional?

sound/soc/sof/pcm.c line 151

Quoted 9 lines
sound/soc/sof/pcm.c new file +9 -0
+	/* validate offset */
+	posn_offset = ipc_params_reply.posn_offset;
+
+	/* check if offset is overflow or it is not aligned */
+	if (posn_offset > sdev->stream_box.size ||
+	    posn_offset % sizeof(struct sof_ipc_stream_posn) != 0) {
+		dev_err(sdev->dev, "error: got wrong posn offset 0x%x for PCM %d\n",
+			posn_offset, ret);
+		return ret;

Here you return an error after checking more things.

sound/soc/sof/pcm.c line 159

Quoted 8 lines
sound/soc/sof/pcm.c new file +8 -0
+	}
+	spcm->posn_offset[substream->stream] =
+		sdev->stream_box.offset + posn_offset;
+
+	/* save pcm hw_params */
+	memcpy(&spcm->params[substream->stream], params, sizeof(*params));
+
+	return ret;

Even here returns an error after saving as if done properly.

Quoted 2 lines
sound/soc/sof/pcm.c new file +2 -0
+static int sof_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
+{

....

sound/soc/sof/pcm.c line 269

Quoted 7 lines
sound/soc/sof/pcm.c new file +7 -0
+	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
+
+		/* check if the stream hw_params needs to be restored */
+		if (spcm->restore_stream[substream->stream]) {
+
+			/* restore hw_params */
+			ret = sof_restore_hw_params(substream, spcm, sdev);

This calls the hw_params that is supposed to be non-atomic.

sound/soc/sof/pcm.c line 317

Quoted 7 lines
sound/soc/sof/pcm.c new file +7 -0
+	snd_sof_pcm_platform_trigger(sdev, substream, cmd);
+
+	/* send IPC to the DSP */
+	ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
+				 sizeof(stream), &reply, sizeof(reply));
+
+	return ret;

... so the whole trigger action is non-atomic PCM only?

thanks,

Takashi

#43 lore raw

Re: [PATCH v3 05/14] ASoC: SOF: Add PCM operations support

Quoted 10 lines · Takashi Iwai

sound/soc/sof/pcm.c line 81

Quoted 6 lines · Pierre-Louis Bossart
sound/soc/sof/pcm.c new file +5 -0
+	/* number of pages should be rounded up */
+	if (runtime->dma_bytes % PAGE_SIZE)
+		pcm.params.buffer.pages = (runtime->dma_bytes / PAGE_SIZE) + 1;
+	else
+		pcm.params.buffer.pages = runtime->dma_bytes / PAGE_SIZE;

There is likely some nice macro for this :)

DIV_ROUND_UP() ?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#44 lore raw

Re: [Sound-open-firmware] [PATCH v3 05/14] ASoC: SOF: Add PCM operations support

In reply to Takashi Iwai
Quoted 8 lines · Takashi Iwai

sound/soc/sof/pcm.c line 81

Quoted 5 lines · Pierre-Louis Bossart
sound/soc/sof/pcm.c new file +5 -0
+	/* number of pages should be rounded up */
+	if (runtime->dma_bytes % PAGE_SIZE)
+		pcm.params.buffer.pages = (runtime->dma_bytes / PAGE_SIZE) + 1;
+	else
+		pcm.params.buffer.pages = runtime->dma_bytes / PAGE_SIZE;

There is likely some nice macro for this :)

Yes, will fix.

Quoted 8 lines

sound/soc/sof/pcm.c line 137

Quoted 7 lines
sound/soc/sof/pcm.c new file +6 -0
+	/* firmware already configured host stream */
+	ret = snd_sof_pcm_platform_hw_params(sdev,
+					     substream,
+					     params,
+					     &pcm.params);
+	dev_dbg(sdev->dev, "stream_tag %d", pcm.params.stream_tag);

This error can be ignored?

will double-check.

Quoted 6 lines

sound/soc/sof/pcm.c line 141

Quoted 4 lines
sound/soc/sof/pcm.c new file +3 -0
+	/* send IPC to the DSP */
+	ret = sof_ipc_tx_message(sdev->ipc, pcm.hdr.cmd, &pcm, sizeof(pcm),
+				 &ipc_params_reply, sizeof(ipc_params_reply));

This error value isn't evaluated immediately but passed until the last. Is it intentional?

Not sure, will double-check. Now that I read the code again this looks odds, and if it was intentional then we need an explicit comment. Thanks for the sighting.

Quoted 12 lines

sound/soc/sof/pcm.c line 151

Quoted 10 lines
sound/soc/sof/pcm.c new file +9 -0
+	/* validate offset */
+	posn_offset = ipc_params_reply.posn_offset;
+
+	/* check if offset is overflow or it is not aligned */
+	if (posn_offset > sdev->stream_box.size ||
+	    posn_offset % sizeof(struct sof_ipc_stream_posn) != 0) {
+		dev_err(sdev->dev, "error: got wrong posn offset 0x%x for PCM %d\n",
+			posn_offset, ret);
+		return ret;

Here you return an error after checking more things.

Quoted 9 lines

sound/soc/sof/pcm.c line 159

Quoted 8 lines
sound/soc/sof/pcm.c new file +8 -0
+	}
+	spcm->posn_offset[substream->stream] =
+		sdev->stream_box.offset + posn_offset;
+
+	/* save pcm hw_params */
+	memcpy(&spcm->params[substream->stream], params, sizeof(*params));
+
+	return ret;

Even here returns an error after saving as if done properly.

will double-check this entire sequence.

Quoted 14 lines
Quoted 4 lines
sound/soc/sof/pcm.c new file +2 -0
+static int sof_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
+{

....

sound/soc/sof/pcm.c line 269

Quoted 7 lines
sound/soc/sof/pcm.c new file +7 -0
+	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
+
+		/* check if the stream hw_params needs to be restored */
+		if (spcm->restore_stream[substream->stream]) {
+
+			/* restore hw_params */
+			ret = sof_restore_hw_params(substream, spcm, sdev);

This calls the hw_params that is supposed to be non-atomic.

Quoted 8 lines

sound/soc/sof/pcm.c line 317

Quoted 7 lines
sound/soc/sof/pcm.c new file +7 -0
+	snd_sof_pcm_platform_trigger(sdev, substream, cmd);
+
+	/* send IPC to the DSP */
+	ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
+				 sizeof(stream), &reply, sizeof(reply));
+
+	return ret;

... so the whole trigger action is non-atomic PCM only?

Not sure if I fully understand your point here. The trigger does indeed need an IPC to proceed, but the front-ends are marked as such with the .nonatomic field set to true. Not sure how different this is from existing atom/sst or Skylake drivers.

#45 lore raw

Re: [Sound-open-firmware] [PATCH v3 05/14] ASoC: SOF: Add PCM operations support

Quoted 9 lines · Pierre-Louis Bossart
Quoted 9 lines

sound/soc/sof/pcm.c line 317

Quoted 8 lines
sound/soc/sof/pcm.c new file +7 -0
+	snd_sof_pcm_platform_trigger(sdev, substream, cmd);
+
+	/* send IPC to the DSP */
+	ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
+				 sizeof(stream), &reply, sizeof(reply));
+
+	return ret;

... so the whole trigger action is non-atomic PCM only?

Quoted 4 lines

Not sure if I fully understand your point here. The trigger does indeed need an IPC to proceed, but the front-ends are marked as such with the .nonatomic field set to true. Not sure how different this is from existing atom/sst or Skylake drivers.

Hm, so this relies purely on FE, and it seems that we have no check whether FE and BE match wrt nonatomic PCM ops. It has to be addressed, but it's a different topic.

At most, you can put some notes in the code that all these are non-atomic ops. This makes it clearer, for example, that spinlock with irqsave is often redundant.

thanks,

Takashi

#46 lore raw

Re: [Sound-open-firmware] [PATCH v3 05/14] ASoC: SOF: Add PCM operations support

Quoted 14 lines · Takashi Iwai
Quoted 8 lines · Pierre-Louis Bossart
Quoted 8 lines

sound/soc/sof/pcm.c line 317

Quoted 7 lines
sound/soc/sof/pcm.c new file +7 -0
+	snd_sof_pcm_platform_trigger(sdev, substream, cmd);
+
+	/* send IPC to the DSP */
+	ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, &stream,
+				 sizeof(stream), &reply, sizeof(reply));
+
+	return ret;

... so the whole trigger action is non-atomic PCM only?

Quoted 4 lines

Not sure if I fully understand your point here. The trigger does indeed need an IPC to proceed, but the front-ends are marked as such with the .nonatomic field set to true. Not sure how different this is from existing atom/sst or Skylake drivers.

Quoted 7 lines

Hm, so this relies purely on FE, and it seems that we have no check whether FE and BE match wrt nonatomic PCM ops. It has to be addressed, but it's a different topic.

At most, you can put some notes in the code that all these are non-atomic ops. This makes it clearer, for example, that spinlock with irqsave is often redundant.

Yes, noted, we'll check the spinlock use and add comments as need when operations are non-atomic. Thanks for the suggestions, much appreciated.

#48 lore raw

PATCH v3 6/14 ASoC: SOF: Add support for loading topologies

In reply to Pierre-Louis Bossart

From: Liam Girdwood <[email protected]>

SOF uses topology to define the DAPM graphs and widgets, DAIs, PCMs and set parameters for init and run time usage. This patch loads topology and maps it to IPC commands that are build the topology on the DSP.

---
 include/sound/sof/topology.h |  256 ++++
 sound/soc/sof/topology.c     | 2775 ++++++++++++++++++++++++++++++++++
 2 files changed, 3031 insertions(+)
 create mode 100644 include/sound/sof/topology.h
 create mode 100644 sound/soc/sof/topology.c
include/sound/sof/topology.h new file +256 -0
new file mode 100644
index 000000000000..155758598ed5
@@ -0,0 +1,256 @@
+/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) */
+/*
+ * This file is provided under a dual BSD/GPLv2 license.  When using or
+ * redistributing this file, you may do so under either license.
+ *
+ * Copyright(c) 2018 Intel Corporation. All rights reserved.
+ */
+
+#ifndef __INCLUDE_SOUND_SOF_TOPOLOGY_H__
+#define __INCLUDE_SOUND_SOF_TOPOLOGY_H__
+
+#include <sound/sof/header.h>
+
+/*
+ * Component
+ */
+
+/* types of component */
+enum sof_comp_type {
+	SOF_COMP_NONE = 0,
+	SOF_COMP_HOST,
+	SOF_COMP_DAI,
+	SOF_COMP_SG_HOST,	/**< scatter gather variant */
+	SOF_COMP_SG_DAI,	/**< scatter gather variant */
+	SOF_COMP_VOLUME,
+	SOF_COMP_MIXER,
+	SOF_COMP_MUX,
+	SOF_COMP_SRC,
+	SOF_COMP_SPLITTER,
+	SOF_COMP_TONE,
+	SOF_COMP_SWITCH,
+	SOF_COMP_BUFFER,
+	SOF_COMP_EQ_IIR,
+	SOF_COMP_EQ_FIR,
+	SOF_COMP_FILEREAD,	/**< host test based file IO */
+	SOF_COMP_FILEWRITE,	/**< host test based file IO */
+};
+
+/* XRUN action for component */
+#define SOF_XRUN_STOP		1	/**< stop stream */
+#define SOF_XRUN_UNDER_ZERO	2	/**< send 0s to sink */
+#define SOF_XRUN_OVER_NULL	4	/**< send data to NULL */
+
+/* create new generic component - SOF_IPC_TPLG_COMP_NEW */
+struct sof_ipc_comp {
+	struct sof_ipc_cmd_hdr hdr;
+	uint32_t id;
+	enum sof_comp_type type;
+	uint32_t pipeline_id;
+
+	/* reserved for future use */
+	uint32_t reserved[2];
+} __packed;
+
+/*
+ * Component Buffers
+ */
+
+/*
+ * SOF memory capabilities, add new ones at the end
+ */
+#define SOF_MEM_CAPS_RAM			(1 << 0)
+#define SOF_MEM_CAPS_ROM			(1 << 1)
+#define SOF_MEM_CAPS_EXT			(1 << 2) /**< external */
+#define SOF_MEM_CAPS_LP			(1 << 3) /**< low power */
+#define SOF_MEM_CAPS_HP			(1 << 4) /**< high performance */
+#define SOF_MEM_CAPS_DMA			(1 << 5) /**< DMA'able */
+#define SOF_MEM_CAPS_CACHE			(1 << 6) /**< cacheable */
+#define SOF_MEM_CAPS_EXEC			(1 << 7) /**< executable */
+
+/* create new component buffer - SOF_IPC_TPLG_BUFFER_NEW */
+struct sof_ipc_buffer {
+	struct sof_ipc_comp comp;
+	uint32_t size;		/**< buffer size in bytes */
+	uint32_t caps;		/**< SOF_MEM_CAPS_ */
+} __packed;
+
+/* generic component config data - must always be after struct sof_ipc_comp */
+struct sof_ipc_comp_config {
+	struct sof_ipc_cmd_hdr hdr;
+	uint32_t periods_sink;	/**< 0 means variable */
+	uint32_t periods_source;	/**< 0 means variable */
+	uint32_t preload_count;	/**< how many periods to preload */
+	uint32_t frame_fmt;		/**< SOF_IPC_FRAME_ */
+	uint32_t xrun_action;
+
+	/* reserved for future use */
+	uint32_t reserved[2];
+} __packed;
+
+/* generic host component */
+struct sof_ipc_comp_host {
+	struct sof_ipc_comp comp;
+	struct sof_ipc_comp_config config;
+	uint32_t direction;	/**< SOF_IPC_STREAM_ */
+	uint32_t no_irq;	/**< don't send periodic IRQ to host/DSP */
+	uint32_t dmac_config; /**< DMA engine specific */
+}  __packed;
+
+/* generic DAI component */
+struct sof_ipc_comp_dai {
+	struct sof_ipc_comp comp;
+	struct sof_ipc_comp_config config;
+	uint32_t direction;	/**< SOF_IPC_STREAM_ */
+	uint32_t dai_index; /**< index of this type dai */
+	uint32_t type;		/**< DAI type - SOF_DAI_ */
+	uint32_t dmac_config; /**< DMA engine specific */
+}  __packed;
+
+/* generic mixer component */
+struct sof_ipc_comp_mixer {
+	struct sof_ipc_comp comp;
+	struct sof_ipc_comp_config config;
+}  __packed;
+
+/* volume ramping types */
+enum sof_volume_ramp {
+	SOF_VOLUME_LINEAR	= 0,
+	SOF_VOLUME_LOG,
+	SOF_VOLUME_LINEAR_ZC,
+	SOF_VOLUME_LOG_ZC,
+};
+
+/* generic volume component */
+struct sof_ipc_comp_volume {
+	struct sof_ipc_comp comp;
+	struct sof_ipc_comp_config config;
+	uint32_t channels;
+	uint32_t min_value;
+	uint32_t max_value;
+	uint32_t ramp;		/**< SOF_VOLUME_ */
+	uint32_t initial_ramp;	/**< ramp space in ms */
+}  __packed;
+
+/* generic SRC component */
+struct sof_ipc_comp_src {
+	struct sof_ipc_comp comp;
+	struct sof_ipc_comp_config config;
+	/* either source or sink rate must be non zero */
+	uint32_t source_rate;	/**< source rate or 0 for variable */
+	uint32_t sink_rate;	/**< sink rate or 0 for variable */
+	uint32_t rate_mask;	/**< SOF_RATE_ supported rates */
+} __packed;
+
+/* generic MUX component */
+struct sof_ipc_comp_mux {
+	struct sof_ipc_comp comp;
+	struct sof_ipc_comp_config config;
+} __packed;
+
+/* generic tone generator component */
+struct sof_ipc_comp_tone {
+	struct sof_ipc_comp comp;
+	struct sof_ipc_comp_config config;
+	int32_t sample_rate;
+	int32_t frequency;
+	int32_t amplitude;
+	int32_t freq_mult;
+	int32_t ampl_mult;
+	int32_t length;
+	int32_t period;
+	int32_t repeats;
+	int32_t ramp_step;
+} __packed;
+
+/** \brief Types of EFFECT */
+enum sof_ipc_effect_type {
+	SOF_EFFECT_NONE = 0,		/**< None */
+	SOF_EFFECT_INTEL_EQFIR,		/**< Intel FIR */
+	SOF_EFFECT_INTEL_EQIIR,		/**< Intel IIR */
+};
+
+/* general purpose EFFECT configuration */
+struct sof_ipc_comp_effect {
+	struct sof_ipc_hdr hdr;
+	uint32_t type;			/** sof_ipc_effect_type */
+} __packed;
+
+/* FIR equalizer component */
+struct sof_ipc_comp_eq_fir {
+	struct sof_ipc_comp comp;
+	struct sof_ipc_comp_config config;
+	uint32_t size;
+
+	/* reserved for future use */
+	uint32_t reserved[8];
+
+	unsigned char data[0];
+} __packed;
+
+/* IIR equalizer component */
+struct sof_ipc_comp_eq_iir {
+	struct sof_ipc_comp comp;
+	struct sof_ipc_comp_config config;
+	uint32_t size;
+
+	/* reserved for future use */
+	uint32_t reserved[8];
+
+	unsigned char data[0];
+} __packed;
+
+/* frees components, buffers and pipelines
+ * SOF_IPC_TPLG_COMP_FREE, SOF_IPC_TPLG_PIPE_FREE, SOF_IPC_TPLG_BUFFER_FREE
+ */
+struct sof_ipc_free {
+	struct sof_ipc_cmd_hdr hdr;
+	uint32_t id;
+} __packed;
+
+struct sof_ipc_comp_reply {
+	struct sof_ipc_reply rhdr;
+	uint32_t id;
+	uint32_t offset;
+} __packed;
+
+/*
+ * Pipeline
+ */
+
+/* new pipeline - SOF_IPC_TPLG_PIPE_NEW */
+struct sof_ipc_pipe_new {
+	struct sof_ipc_cmd_hdr hdr;
+	uint32_t comp_id;	/**< component id for pipeline */
+	uint32_t pipeline_id;	/**< pipeline id */
+	uint32_t sched_id;	/**< Scheduling component id */
+	uint32_t core;		/**< core we run on */
+	uint32_t deadline;	/**< execution completion deadline in us*/
+	uint32_t priority;	/**< priority level 0 (low) to 10 (max) */
+	uint32_t period_mips;	/**< worst case instruction count per period */
+	uint32_t frames_per_sched;/**< output frames of pipeline, 0 is variable */
+	uint32_t xrun_limit_usecs; /**< report xruns greater than limit */
+
+	/* non zero if timer scheduled, otherwise DAI DMA irq scheduled */
+	uint32_t timer_delay;
+}  __packed;
+
+/* pipeline construction complete - SOF_IPC_TPLG_PIPE_COMPLETE */
+struct sof_ipc_pipe_ready {
+	struct sof_ipc_cmd_hdr hdr;
+	uint32_t comp_id;
+}  __packed;
+
+struct sof_ipc_pipe_free {
+	struct sof_ipc_cmd_hdr hdr;
+	uint32_t comp_id;
+}  __packed;
+
+/* connect two components in pipeline - SOF_IPC_TPLG_COMP_CONNECT */
+struct sof_ipc_pipe_comp_connect {
+	struct sof_ipc_cmd_hdr hdr;
+	uint32_t source_id;
+	uint32_t sink_id;
+}  __packed;
+
+#endif
sound/soc/sof/topology.c new file +2775 -0

2,744 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.17.1

Part of the diff is not shown here. View the raw message.

#49 lore raw

PATCH v3 7/14 ASoC: SOF: Add DSP firmware logger support

In reply to Pierre-Louis Bossart

From: Liam Girdwood <[email protected]>

This patch adds support for real-time DSP logging (timestamped events and bespoke binary data) for firmware debug. The current solution relies on DMA transfers to system memory that is then accessed by userspace tools such as sof-logger. For Intel platforms, two types of DMAs are currently used (GP-DMA for Baytrail/CherryTrail and HDaudio DMA for SKL+)

Due to historical reasons, the driver code follows the DSP firmware conventions and refers to 'traces', but it is currently unrelated to the Linux trace subsystem. Future solutions will include support for more advanced hardware (e.g. MIPI Sys-T), additional formats and the ability to enable/disable specific traces dynamically.

---
 sound/soc/sof/trace.c | 295 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 295 insertions(+)
 create mode 100644 sound/soc/sof/trace.c
sound/soc/sof/trace.c new file +295 -0
new file mode 100644
index 000000000000..9871a174f9ca
@@ -0,0 +1,295 @@
+// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
+//
+// This file is provided under a dual BSD/GPLv2 license.  When using or
+// redistributing this file, you may do so under either license.
+//
+// Copyright(c) 2018 Intel Corporation. All rights reserved.
+//
+// Author: Liam Girdwood <[email protected]>
+//
+
+#include <linux/debugfs.h>
+#include <linux/sched/signal.h>
+#include "sof-priv.h"
+#include "ops.h"
+
+static size_t sof_wait_trace_avail(struct snd_sof_dev *sdev,
+				   loff_t pos, size_t buffer_size)
+{
+	wait_queue_entry_t wait;
+
+	/*
+	 * If host offset is less than local pos, it means write pointer of
+	 * host DMA buffer has been wrapped. We should output the trace data
+	 * at the end of host DMA buffer at first.
+	 */
+	if (sdev->host_offset < pos)
+		return buffer_size - pos;
+
+	/* If there is available trace data now, it is unnecessary to wait. */
+	if (sdev->host_offset > pos)
+		return sdev->host_offset - pos;
+
+	/* wait for available trace data from FW */
+	init_waitqueue_entry(&wait, current);
+	set_current_state(TASK_INTERRUPTIBLE);
+	add_wait_queue(&sdev->trace_sleep, &wait);
+
+	if (signal_pending(current)) {
+		remove_wait_queue(&sdev->trace_sleep, &wait);
+		goto out;
+	}
+
+	/* set timeout to max value, no error code */
+	schedule_timeout(MAX_SCHEDULE_TIMEOUT);
+	remove_wait_queue(&sdev->trace_sleep, &wait);
+
+out:
+	/* return bytes available for copy */
+	if (sdev->host_offset < pos)
+		return buffer_size - pos;
+
+	return sdev->host_offset - pos;
+}
+
+static ssize_t sof_dfsentry_trace_read(struct file *file, char __user *buffer,
+				       size_t count, loff_t *ppos)
+{
+	struct snd_sof_dfsentry_buf *dfse = file->private_data;
+	struct snd_sof_dev *sdev = dfse->sdev;
+	unsigned long rem;
+	loff_t lpos = *ppos;
+	size_t avail, buffer_size = dfse->size;
+	u64 lpos_64;
+
+	/* make sure we know about any failures on the DSP side */
+	sdev->dtrace_error = false;
+
+	/* check pos and count */
+	if (lpos < 0)
+		return -EINVAL;
+	if (!count)
+		return 0;
+
+	/* check for buffer wrap and count overflow */
+	lpos_64 = lpos;
+	lpos = do_div(lpos_64, buffer_size);
+
+	if (count > buffer_size - lpos)
+		count = buffer_size - lpos;
+
+	/* get available count based on current host offset */
+	avail = sof_wait_trace_avail(sdev, lpos, buffer_size);
+	if (sdev->dtrace_error) {
+		dev_err(sdev->dev, "error: trace IO error\n");
+		return -EIO;
+	}
+
+	/* make sure count is <= avail */
+	count = avail > count ? count : avail;
+
+	/* copy available trace data to debugfs */
+	rem = copy_to_user(buffer, dfse->buf + lpos, count);
+	if (rem == count)
+		return -EFAULT;
+
+	*ppos += count;
+
+	/* move debugfs reading position */
+	return count;
+}
+
+static const struct file_operations sof_dfs_trace_fops = {
+	.open = simple_open,
+	.read = sof_dfsentry_trace_read,
+	.llseek = default_llseek,
+};
+
+static int trace_debugfs_create(struct snd_sof_dev *sdev)
+{
+	struct snd_sof_dfsentry_buf *dfse;
+
+	if (!sdev)
+		return -EINVAL;
+
+	dfse = kzalloc(sizeof(*dfse), GFP_KERNEL);
+	if (!dfse)
+		return -ENOMEM;
+
+	dfse->buf = sdev->dmatb.area;
+	dfse->size = sdev->dmatb.bytes;
+	dfse->sdev = sdev;
+
+	dfse->dfsentry = debugfs_create_file("trace", 0444, sdev->debugfs_root,
+					     dfse, &sof_dfs_trace_fops);
+	if (!dfse->dfsentry) {
+		dev_err(sdev->dev,
+			"error: cannot create debugfs entry for trace\n");
+		kfree(dfse);
+		return -ENODEV;
+	}
+
+	return 0;
+}
+
+int snd_sof_init_trace_ipc(struct snd_sof_dev *sdev)
+{
+	struct sof_ipc_dma_trace_params params;
+	struct sof_ipc_reply ipc_reply;
+	int ret;
+
+	if (sdev->dtrace_is_enabled)
+		return -EINVAL;
+
+	/* set IPC parameters */
+	params.hdr.size = sizeof(params);
+	params.hdr.cmd = SOF_IPC_GLB_TRACE_MSG | SOF_IPC_TRACE_DMA_PARAMS;
+	params.buffer.phy_addr = sdev->dmatp.addr;
+	params.buffer.size = sdev->dmatb.bytes;
+	params.buffer.offset = 0;
+	params.buffer.pages = sdev->dma_trace_pages;
+	params.stream_tag = 0;
+
+	sdev->host_offset = 0;
+
+	ret = snd_sof_dma_trace_init(sdev, &params.stream_tag);
+	if (ret < 0) {
+		dev_err(sdev->dev,
+			"error: fail in snd_sof_dma_trace_init %d\n", ret);
+		return ret;
+	}
+	dev_dbg(sdev->dev, "stream_tag: %d\n", params.stream_tag);
+
+	/* send IPC to the DSP */
+	ret = sof_ipc_tx_message(sdev->ipc,
+				 params.hdr.cmd, &params, sizeof(params),
+				 &ipc_reply, sizeof(ipc_reply));
+	if (ret < 0) {
+		dev_err(sdev->dev,
+			"error: can't set params for DMA for trace %d\n", ret);
+		return ret;
+	}
+
+	ret = snd_sof_dma_trace_trigger(sdev, SNDRV_PCM_TRIGGER_START);
+	if (ret < 0) {
+		dev_err(sdev->dev,
+			"error: snd_sof_dma_trace_trigger: start: %d\n", ret);
+		return ret;
+	}
+
+	sdev->dtrace_is_enabled = true;
+
+	return 0;
+}
+
+int snd_sof_init_trace(struct snd_sof_dev *sdev)
+{
+	int ret;
+
+	/* set false before start initialization */
+	sdev->dtrace_is_enabled = false;
+
+	/* allocate trace page table buffer */
+	ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->parent,
+				  PAGE_SIZE, &sdev->dmatp);
+	if (ret < 0) {
+		dev_err(sdev->dev,
+			"error: can't alloc page table for trace %d\n", ret);
+		return ret;
+	}
+
+	/* allocate trace data buffer */
+	ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV_SG, sdev->parent,
+				  DMA_BUF_SIZE_FOR_TRACE, &sdev->dmatb);
+	if (ret < 0) {
+		dev_err(sdev->dev,
+			"error: can't alloc buffer for trace %d\n", ret);
+		goto page_err;
+	}
+
+	/* craete compressed page table for audio firmware */
+	ret = snd_sof_create_page_table(sdev, &sdev->dmatb, sdev->dmatp.area,
+					sdev->dmatb.bytes);
+	if (ret < 0)
+		goto table_err;
+
+	sdev->dma_trace_pages = ret;
+	dev_dbg(sdev->dev, "dma_trace_pages: %d\n", sdev->dma_trace_pages);
+
+	if (sdev->first_boot) {
+		ret = trace_debugfs_create(sdev);
+		if (ret < 0)
+			goto table_err;
+	}
+
+	init_waitqueue_head(&sdev->trace_sleep);
+
+	ret = snd_sof_init_trace_ipc(sdev);
+	if (ret < 0)
+		goto table_err;
+
+	return 0;
+table_err:
+	snd_dma_free_pages(&sdev->dmatb);
+page_err:
+	snd_dma_free_pages(&sdev->dmatp);
+	return ret;
+}
+EXPORT_SYMBOL(snd_sof_init_trace);
+
+int snd_sof_trace_update_pos(struct snd_sof_dev *sdev,
+			     struct sof_ipc_dma_trace_posn *posn)
+{
+	if (sdev->dtrace_is_enabled && sdev->host_offset != posn->host_offset) {
+		sdev->host_offset = posn->host_offset;
+		wake_up(&sdev->trace_sleep);
+	}
+
+	if (posn->overflow != 0)
+		dev_err(sdev->dev,
+			"error: DSP trace buffer overflow %u bytes. Total messages %d\n",
+			posn->overflow, posn->messages);
+
+	return 0;
+}
+
+/* an error has occurred within the DSP that prevents further trace */
+void snd_sof_trace_notify_for_error(struct snd_sof_dev *sdev)
+{
+	if (sdev->dtrace_is_enabled) {
+		dev_err(sdev->dev, "error: waking up any trace sleepers\n");
+		sdev->dtrace_error = true;
+		wake_up(&sdev->trace_sleep);
+	}
+}
+EXPORT_SYMBOL(snd_sof_trace_notify_for_error);
+
+void snd_sof_release_trace(struct snd_sof_dev *sdev)
+{
+	int ret;
+
+	if (!sdev->dtrace_is_enabled)
+		return;
+
+	ret = snd_sof_dma_trace_trigger(sdev, SNDRV_PCM_TRIGGER_STOP);
+	if (ret < 0)
+		dev_err(sdev->dev,
+			"error: snd_sof_dma_trace_trigger: stop: %d\n", ret);
+
+	ret = snd_sof_dma_trace_release(sdev);
+	if (ret < 0)
+		dev_err(sdev->dev,
+			"error: fail in snd_sof_dma_trace_release %d\n", ret);
+
+	sdev->dtrace_is_enabled = false;
+}
+EXPORT_SYMBOL(snd_sof_release_trace);
+
+void snd_sof_free_trace(struct snd_sof_dev *sdev)
+{
+	snd_sof_release_trace(sdev);
+
+	snd_dma_free_pages(&sdev->dmatb);
+	snd_dma_free_pages(&sdev->dmatp);
+}
+EXPORT_SYMBOL(snd_sof_free_trace);
Signature · 1 line
2.17.1
#50 lore raw

Re: [PATCH v3 07/14] ASoC: SOF: Add DSP firmware logger support

Quoted 15 lines · Pierre-Louis Bossart

From: Liam Girdwood <[email protected]>

This patch adds support for real-time DSP logging (timestamped events and bespoke binary data) for firmware debug. The current solution relies on DMA transfers to system memory that is then accessed by userspace tools such as sof-logger. For Intel platforms, two types of DMAs are currently used (GP-DMA for Baytrail/CherryTrail and HDaudio DMA for SKL+)

Due to historical reasons, the driver code follows the DSP firmware conventions and refers to 'traces', but it is currently unrelated to the Linux trace subsystem. Future solutions will include support for more advanced hardware (e.g. MIPI Sys-T), additional formats and the ability to enable/disable specific traces dynamically.

sound/soc/sof/trace.c line 79

Quoted 2 lines
sound/soc/sof/trace.c new file +2 -0
+	if (count > buffer_size - lpos)
+		count = buffer_size - lpos;

min() / max() ?

sound/soc/sof/trace.c line 89

Quoted 2 lines
sound/soc/sof/trace.c new file +2 -0
+	/* make sure count is <= avail */
+	count = avail > count ? count : avail;

ditto.
Check entire series for such.

sound/soc/sof/trace.c line 129

Quoted 8 lines
sound/soc/sof/trace.c new file +7 -0
+	dfse->dfsentry = debugfs_create_file("trace", 0444, sdev->debugfs_root,
+					     dfse, &sof_dfs_trace_fops);
+	if (!dfse->dfsentry) {
+		dev_err(sdev->dev,
+			"error: cannot create debugfs entry for trace\n");
+		kfree(dfse);
 
+		return -ENODEV;

Should not return an error. We must be fine w/o debugfs files.

Quoted 4 lines
sound/soc/sof/trace.c new file +4 -0
+	}
+
+	return 0;
+}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#51 lore raw

Re: [PATCH v3 07/14] ASoC: SOF: Add DSP firmware logger support

Quoted 19 lines · Andy Shevchenko
Quoted 14 lines · Pierre-Louis Bossart

From: Liam Girdwood <[email protected]>

This patch adds support for real-time DSP logging (timestamped events and bespoke binary data) for firmware debug. The current solution relies on DMA transfers to system memory that is then accessed by userspace tools such as sof-logger. For Intel platforms, two types of DMAs are currently used (GP-DMA for Baytrail/CherryTrail and HDaudio DMA for SKL+)

Due to historical reasons, the driver code follows the DSP firmware conventions and refers to 'traces', but it is currently unrelated to the Linux trace subsystem. Future solutions will include support for more advanced hardware (e.g. MIPI Sys-T), additional formats and the ability to enable/disable specific traces dynamically.

sound/soc/sof/trace.c line 79

Quoted 2 lines
sound/soc/sof/trace.c new file +2 -0
+	if (count > buffer_size - lpos)
+		count = buffer_size - lpos;

min() / max() ?

Quoted 4 lines

sound/soc/sof/trace.c line 89

Quoted 2 lines
sound/soc/sof/trace.c new file +2 -0
+	/* make sure count is <= avail */
+	count = avail > count ? count : avail;

ditto.
Check entire series for such.

yep.

Quoted 9 lines

sound/soc/sof/trace.c line 129

Quoted 8 lines
sound/soc/sof/trace.c new file +7 -0
+	dfse->dfsentry = debugfs_create_file("trace", 0444, sdev->debugfs_root,
+					     dfse, &sof_dfs_trace_fops);
+	if (!dfse->dfsentry) {
+		dev_err(sdev->dev,
+			"error: cannot create debugfs entry for trace\n");
+		kfree(dfse);
+		return -ENODEV;

Should not return an error. We must be fine w/o debugfs files.

Not sure I fully agree with the 'must'.

yes execution should proceed, but without debugfs we cannot, well, debug, so this is a real show-stopper

Quoted 5 lines
Quoted 5 lines
sound/soc/sof/trace.c new file +4 -0
+	}
+
+	return 0;
+}
#52 lore raw

Re: [PATCH v3 07/14] ASoC: SOF: Add DSP firmware logger support

Quoted 37 lines · Pierre-Louis Bossart
Quoted 19 lines · Andy Shevchenko
Quoted 14 lines · Pierre-Louis Bossart

From: Liam Girdwood <[email protected]>

This patch adds support for real-time DSP logging (timestamped events and bespoke binary data) for firmware debug. The current solution relies on DMA transfers to system memory that is then accessed by userspace tools such as sof-logger. For Intel platforms, two types of DMAs are currently used (GP-DMA for Baytrail/CherryTrail and HDaudio DMA for SKL+)

Due to historical reasons, the driver code follows the DSP firmware conventions and refers to 'traces', but it is currently unrelated to the Linux trace subsystem. Future solutions will include support for more advanced hardware (e.g. MIPI Sys-T), additional formats and the ability to enable/disable specific traces dynamically.

sound/soc/sof/trace.c line 79

Quoted 2 lines
sound/soc/sof/trace.c new file +2 -0
+	if (count > buffer_size - lpos)
+		count = buffer_size - lpos;

min() / max() ?

Quoted 4 lines

sound/soc/sof/trace.c line 89

Quoted 2 lines
sound/soc/sof/trace.c new file +2 -0
+	/* make sure count is <= avail */
+	count = avail > count ? count : avail;

ditto.
Check entire series for such.

yep.

Quoted 9 lines

sound/soc/sof/trace.c line 129

Quoted 8 lines
sound/soc/sof/trace.c new file +7 -0
+	dfse->dfsentry = debugfs_create_file("trace", 0444, sdev->debugfs_root,
+					     dfse, &sof_dfs_trace_fops);
+	if (!dfse->dfsentry) {
+		dev_err(sdev->dev,
+			"error: cannot create debugfs entry for trace\n");
+		kfree(dfse);
+		return -ENODEV;

Should not return an error. We must be fine w/o debugfs files.

Quoted 4 lines

Not sure I fully agree with the 'must'.

yes execution should proceed, but without debugfs we cannot, well, debug, so this is a real show-stopper

So the caller would ignore the error from this function?

Note that debugfs is a pure optional matter, and you can build a kernel without CONFIG_DEBUGFS, too. Then you'll always get this error...

thanks,

Takashi

#53 lore raw

Re: [PATCH v3 07/14] ASoC: SOF: Add DSP firmware logger support

In reply to Pierre-Louis Bossart
Quoted 10 lines · Pierre-Louis Bossart

From: Liam Girdwood <[email protected]>

This patch adds support for real-time DSP logging (timestamped events and bespoke binary data) for firmware debug. The current solution relies on DMA transfers to system memory that is then accessed by userspace tools such as sof-logger. For Intel platforms, two types of DMAs are currently used (GP-DMA for Baytrail/CherryTrail and HDaudio DMA for SKL+)

Quoted 5 lines

Due to historical reasons, the driver code follows the DSP firmware conventions and refers to 'traces', but it is currently unrelated to the Linux trace subsystem. Future solutions will include support for more advanced hardware (e.g. MIPI Sys-T), additional formats and the ability to enable/disable specific traces dynamically.

So what's the reason not to use Linux standard tracing infrastructure?

thanks,

Takashi

#54 lore raw

Re: [Sound-open-firmware] [PATCH v3 07/14] ASoC: SOF: Add DSP firmware logger support

Quoted 11 lines · Takashi Iwai
Quoted 9 lines · Pierre-Louis Bossart

From: Liam Girdwood <[email protected]>

This patch adds support for real-time DSP logging (timestamped events and bespoke binary data) for firmware debug. The current solution relies on DMA transfers to system memory that is then accessed by userspace tools such as sof-logger. For Intel platforms, two types of DMAs are currently used (GP-DMA for Baytrail/CherryTrail and HDaudio DMA for SKL+)

Quoted 6 lines
Quoted 5 lines

Due to historical reasons, the driver code follows the DSP firmware conventions and refers to 'traces', but it is currently unrelated to the Linux trace subsystem. Future solutions will include support for more advanced hardware (e.g. MIPI Sys-T), additional formats and the ability to enable/disable specific traces dynamically.

So what's the reason not to use Linux standard tracing infrastructure?

I obviously failed to convey the intent in the commit message :-(

What we have today is just a DMA-based transfers of 'trace' data into a ring buffer. That's it. it's very similar to what always existed on Atom and Skylake, just more transparent and released for upstream reviews this time.

Is it optimal or final? Absolutely not. There will be evolutions such as

A) support for multi-cores on the DSP side, each with their own 'trace' capability.

B) support for other hardware platforms which may not have a DMA.

C) support for 'probes' to retrieve and inject PCM data into specific firmware nodes.

This patch does not create a new generic tracing infrastructure for Linux. We are exploring ways by which this standard tracing infrastructure can be used, we just haven't had time to look into it as we focused on runtime_pm and new platforms first.

Also we need to make sure the DSP traces are not defined for Linux only, it's intended that the SOF firmware is used in non-Linux environments, so we want to use what Linux provides, but not constrain SOF to work for Linux only.

Does this help?

#55 lore raw

Re: [Sound-open-firmware] [PATCH v3 07/14] ASoC: SOF: Add DSP firmware logger support

Quoted 43 lines · Pierre-Louis Bossart
Quoted 11 lines · Takashi Iwai
Quoted 9 lines · Pierre-Louis Bossart

From: Liam Girdwood <[email protected]>

This patch adds support for real-time DSP logging (timestamped events and bespoke binary data) for firmware debug. The current solution relies on DMA transfers to system memory that is then accessed by userspace tools such as sof-logger. For Intel platforms, two types of DMAs are currently used (GP-DMA for Baytrail/CherryTrail and HDaudio DMA for SKL+)

Quoted 6 lines
Quoted 5 lines

Due to historical reasons, the driver code follows the DSP firmware conventions and refers to 'traces', but it is currently unrelated to the Linux trace subsystem. Future solutions will include support for more advanced hardware (e.g. MIPI Sys-T), additional formats and the ability to enable/disable specific traces dynamically.

So what's the reason not to use Linux standard tracing infrastructure?

I obviously failed to convey the intent in the commit message :-(

What we have today is just a DMA-based transfers of 'trace' data into a ring buffer. That's it. it's very similar to what always existed on Atom and Skylake, just more transparent and released for upstream reviews this time.

Is it optimal or final? Absolutely not. There will be evolutions such as

A) support for multi-cores on the DSP side, each with their own 'trace' capability.

B) support for other hardware platforms which may not have a DMA.

C) support for 'probes' to retrieve and inject PCM data into specific firmware nodes.

This patch does not create a new generic tracing infrastructure for Linux. We are exploring ways by which this standard tracing infrastructure can be used, we just haven't had time to look into it as we focused on runtime_pm and new platforms first.

Quoted 6 lines

Also we need to make sure the DSP traces are not defined for Linux only, it's intended that the SOF firmware is used in non-Linux environments, so we want to use what Linux provides, but not constrain SOF to work for Linux only.

Does this help?

OK, that's fine. Liam and I talked about Linux tracing for SOF, and I thought this might be that. But this is rather a hardware data logging, so it's a different beast.

thanks,

Takashi

#56 lore raw

Re: [Sound-open-firmware] [PATCH v3 07/14] ASoC: SOF: Add DSP firmware logger support

Quoted 48 lines · Takashi Iwai
Quoted 46 lines · Pierre-Louis Bossart
Quoted 11 lines · Takashi Iwai
Quoted 9 lines · Pierre-Louis Bossart

From: Liam Girdwood <[email protected]>

This patch adds support for real-time DSP logging (timestamped events and bespoke binary data) for firmware debug. The current solution relies on DMA transfers to system memory that is then accessed by userspace tools such as sof-logger. For Intel platforms, two types of DMAs are currently used (GP-DMA for Baytrail/CherryTrail and HDaudio DMA for SKL+)

Quoted 6 lines
Quoted 5 lines

Due to historical reasons, the driver code follows the DSP firmware conventions and refers to 'traces', but it is currently unrelated to the Linux trace subsystem. Future solutions will include support for more advanced hardware (e.g. MIPI Sys-T), additional formats and the ability to enable/disable specific traces dynamically.

So what's the reason not to use Linux standard tracing infrastructure?

I obviously failed to convey the intent in the commit message :-(

What we have today is just a DMA-based transfers of 'trace' data into a ring buffer. That's it. it's very similar to what always existed on Atom and Skylake, just more transparent and released for upstream reviews this time.

Is it optimal or final? Absolutely not. There will be evolutions such as

A) support for multi-cores on the DSP side, each with their own 'trace' capability.

B) support for other hardware platforms which may not have a DMA.

C) support for 'probes' to retrieve and inject PCM data into specific firmware nodes.

This patch does not create a new generic tracing infrastructure for Linux. We are exploring ways by which this standard tracing infrastructure can be used, we just haven't had time to look into it as we focused on runtime_pm and new platforms first.

Also we need to make sure the DSP traces are not defined for Linux only, it's intended that the SOF firmware is used in non-Linux environments, so we want to use what Linux provides, but not constrain SOF to work for Linux only.

Quoted 4 lines
Quoted 1 line

Does this help?

OK, that's fine. Liam and I talked about Linux tracing for SOF, and I thought this might be that. But this is rather a hardware data logging, so it's a different beast.

Thanks. I'll rework the commit message for the third time to be clearer...

maintainer
#57 lore raw

Re: [PATCH v3 07/14] ASoC: SOF: Add DSP firmware logger support

In reply to Pierre-Louis Bossart

sound/soc/sof/trace.c line 89

Quoted 2 lines · Pierre-Louis Bossart
sound/soc/sof/trace.c new file +2 -0
+	/* make sure count is <= avail */
+	count = avail > count ? count : avail;

min()?

#58 lore raw

Re: [PATCH v3 07/14] ASoC: SOF: Add DSP firmware logger support

Quoted 5 lines · Mark Brown

sound/soc/sof/trace.c line 89

Quoted 2 lines · Pierre-Louis Bossart
sound/soc/sof/trace.c new file +2 -0
+	/* make sure count is <= avail */
+	count = avail > count ? count : avail;

min()?

I tried to use min() but then Sparse started complaining so went back to an explicit test+assign.

I don't have a strong opinion on this, but we've found so many improvements with the tools that I tend to favor warning-free code.

maintainer
#59 lore raw

Re: [PATCH v3 07/14] ASoC: SOF: Add DSP firmware logger support

Quoted 8 lines · Pierre-Louis Bossart
Quoted 6 lines · Mark Brown

sound/soc/sof/trace.c line 89

Quoted 2 lines · Pierre-Louis Bossart
sound/soc/sof/trace.c new file +2 -0
+	/* make sure count is <= avail */
+	count = avail > count ? count : avail;

min()?

Quoted 5 lines

I tried to use min() but then Sparse started complaining so went back to an explicit test+assign.

I don't have a strong opinion on this, but we've found so many improvements with the tools that I tend to favor warning-free code.

Why was sparse complaining - was it spotting something that is an actual issue here and you've just masked the warning?

#60 lore raw

Re: [PATCH v3 07/14] ASoC: SOF: Add DSP firmware logger support

Quoted 10 lines
Quoted 8 lines
Quoted 4 lines

sound/soc/sof/trace.c line 89

Quoted 3 lines
sound/soc/sof/trace.c new file +2 -0
+	/* make sure count is <= avail */
+	count = avail > count ? count : avail;

min()?

I tried to use min() but then Sparse started complaining so went back to an explicit test+assign.
I don't have a strong opinion on this, but we've found so many improvements with the tools that I tend to favor warning-free code.

Why was sparse complaining - was it spotting something that is an actual issue here and you've just masked the warning?

It was about the use of typeof/sizeof() in min(), not an actual issue in the code

sound/soc/sof/trace.c:90:17: warning: expression using sizeof(void) sound/soc/sof/trace.c:90:17: warning: expression using sizeof(void)

maintainer
#61 lore raw

Re: [PATCH v3 07/14] ASoC: SOF: Add DSP firmware logger support

Quoted 5 lines · Pierre-Louis Bossart
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

sound/soc/sof/trace.c line 89

Quoted 3 lines
sound/soc/sof/trace.c new file +2 -0
+	/* make sure count is <= avail */
+	count = avail > count ? count : avail;

min()?

Quoted 8 lines
Quoted 2 lines

Why was sparse complaining - was it spotting something that is an actual issue here and you've just masked the warning?

It was about the use of typeof/sizeof() in min(), not an actual issue in the code

sound/soc/sof/trace.c:90:17: warning: expression using sizeof(void) sound/soc/sof/trace.c:90:17: warning: expression using sizeof(void)

I don't understand that warning - avail and count are both size_t so where's it deciding that there's a void involved?

#62 lore raw

Re: [PATCH v3 07/14] ASoC: SOF: Add DSP firmware logger support

Quoted 9 lines
Quoted 7 lines
Quoted 3 lines

Why was sparse complaining - was it spotting something that is an actual issue here and you've just masked the warning?

It was about the use of typeof/sizeof() in min(), not an actual issue in the code
sound/soc/sof/trace.c:90:17: warning: expression using sizeof(void) sound/soc/sof/trace.c:90:17: warning: expression using sizeof(void)

I don't understand that warning - avail and count are both size_t so where's it deciding that there's a void involved?

no idea really, I am only a user of sparse, I don't know how it generated that warning.

there are equally confusing ones such as

include/linux/slab.h:332:43: warning: dubious: x & !y

include/linux/slab.h:665:13: warning: call with no type!

include/linux/slab.h:665:13: error: undefined identifier '__builtin_mul_overflow

The main benefit of sparse is that it is the only tool that found endianess and iomem space issues, but it's often buried in heaps of false alarms in include files.

maintainer
#63 lore raw

Re: [PATCH v3 07/14] ASoC: SOF: Add DSP firmware logger support

Quoted 5 lines · Pierre-Louis Bossart
Quoted 2 lines

I don't understand that warning - avail and count are both size_t so where's it deciding that there's a void involved?

no idea really, I am only a user of sparse, I don't know how it generated that warning.

Yeah, I use it quite a bit as well.

Quoted 4 lines

there are equally confusing ones such as

include/linux/slab.h:332:43: warning: dubious: x & !y

Quoted 8 lines

include/linux/slab.h:665:13: warning: call with no type!

include/linux/slab.h:665:13: error: undefined identifier '__builtin_mul_overflow

The main benefit of sparse is that it is the only tool that found endianess and iomem space issues, but it's often buried in heaps of false alarms in include files.

It's got less and less useful as time goes on I think :( For this one did you try reporting the bug to the authors?

#64 lore raw

Re: [PATCH v3 07/14] ASoC: SOF: Add DSP firmware logger support

Quoted 3 lines

It's got less and less useful as time goes on I think :( For this one did you try reporting the bug to the authors?

Nope, mostly because I don't even know who the contributors are these days.

Will report this when I get a chance (trying to submit another SOF update before Chinese New Year).

#65 lore raw

PATCH v3 8/14 ASoC: SOF: Add DSP HW abstraction operations

In reply to Pierre-Louis Bossart

From: Liam Girdwood <[email protected]>

Add operation pointers that can be called by core to control a wide variety of DSP targets. The DSP HW drivers will fill in these operations.

---
 sound/soc/sof/ops.c | 200 +++++++++++++++++++++++
 sound/soc/sof/ops.h | 379 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 579 insertions(+)
 create mode 100644 sound/soc/sof/ops.c
 create mode 100644 sound/soc/sof/ops.h
sound/soc/sof/ops.c new file +200 -0
new file mode 100644
index 000000000000..41143fd97772
@@ -0,0 +1,200 @@
+// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
+//
+// This file is provided under a dual BSD/GPLv2 license.  When using or
+// redistributing this file, you may do so under either license.
+//
+// Copyright(c) 2018 Intel Corporation. All rights reserved.
+//
+// Author: Liam Girdwood <[email protected]>
+//
+
+#include <linux/pci.h>
+#include "ops.h"
+
+int snd_sof_pci_update_bits_unlocked(struct snd_sof_dev *sdev, u32 offset,
+				     u32 mask, u32 value)
+{
+	bool change;
+	unsigned int old, new;
+	u32 ret = ~0; /* explicit init to remove uninitialized use warnings */
+
+	pci_read_config_dword(sdev->pci, offset, &ret);
+	dev_dbg(sdev->dev, "Debug PCIR: %8.8x at  %8.8x\n",
+		pci_read_config_dword(sdev->pci, offset, &ret), offset);
+
+	old = ret;
+	new = (old & (~mask)) | (value & mask);
+
+	change = (old != new);
+	if (change) {
+		pci_write_config_dword(sdev->pci, offset, new);
+		dev_dbg(sdev->dev, "Debug PCIW: %8.8x at  %8.8x\n", value,
+			offset);
+	}
+
+	return change;
+}
+EXPORT_SYMBOL(snd_sof_pci_update_bits_unlocked);
+
+int snd_sof_pci_update_bits(struct snd_sof_dev *sdev, u32 offset,
+			    u32 mask, u32 value)
+{
+	unsigned long flags;
+	bool change;
+
+	spin_lock_irqsave(&sdev->hw_lock, flags);
+	change = snd_sof_pci_update_bits_unlocked(sdev, offset, mask, value);
+	spin_unlock_irqrestore(&sdev->hw_lock, flags);
+	return change;
+}
+EXPORT_SYMBOL(snd_sof_pci_update_bits);
+
+int snd_sof_dsp_update_bits_unlocked(struct snd_sof_dev *sdev, u32 bar,
+				     u32 offset, u32 mask, u32 value)
+{
+	bool change;
+	unsigned int old, new;
+	u32 ret;
+
+	ret = snd_sof_dsp_read(sdev, bar, offset);
+
+	old = ret;
+	new = (old & (~mask)) | (value & mask);
+
+	change = (old != new);
+	if (change)
+		snd_sof_dsp_write(sdev, bar, offset, new);
+
+	return change;
+}
+EXPORT_SYMBOL(snd_sof_dsp_update_bits_unlocked);
+
+int snd_sof_dsp_update_bits64_unlocked(struct snd_sof_dev *sdev, u32 bar,
+				       u32 offset, u64 mask, u64 value)
+{
+	bool change;
+	u64 old, new;
+
+	old = snd_sof_dsp_read64(sdev, bar, offset);
+
+	new = (old & (~mask)) | (value & mask);
+
+	change = (old != new);
+	if (change)
+		snd_sof_dsp_write64(sdev, bar, offset, new);
+
+	return change;
+}
+EXPORT_SYMBOL(snd_sof_dsp_update_bits64_unlocked);
+
+/* This is for registers bits with attribute RWC */
+void snd_sof_dsp_update_bits_forced_unlocked(struct snd_sof_dev *sdev, u32 bar,
+					     u32 offset, u32 mask, u32 value)
+{
+	unsigned int old, new;
+	u32 ret;
+
+	ret = snd_sof_dsp_read(sdev, bar, offset);
+
+	old = ret;
+	new = (old & (~mask)) | (value & mask);
+
+	snd_sof_dsp_write(sdev, bar, offset, new);
+}
+EXPORT_SYMBOL(snd_sof_dsp_update_bits_forced_unlocked);
+
+int snd_sof_dsp_update_bits(struct snd_sof_dev *sdev, u32 bar, u32 offset,
+			    u32 mask, u32 value)
+{
+	unsigned long flags;
+	bool change;
+
+	spin_lock_irqsave(&sdev->hw_lock, flags);
+	change = snd_sof_dsp_update_bits_unlocked(sdev, bar, offset, mask,
+						  value);
+	spin_unlock_irqrestore(&sdev->hw_lock, flags);
+	return change;
+}
+EXPORT_SYMBOL(snd_sof_dsp_update_bits);
+
+int snd_sof_dsp_update_bits64(struct snd_sof_dev *sdev, u32 bar, u32 offset,
+			      u64 mask, u64 value)
+{
+	unsigned long flags;
+	bool change;
+
+	spin_lock_irqsave(&sdev->hw_lock, flags);
+	change = snd_sof_dsp_update_bits64_unlocked(sdev, bar, offset, mask,
+						    value);
+	spin_unlock_irqrestore(&sdev->hw_lock, flags);
+	return change;
+}
+EXPORT_SYMBOL(snd_sof_dsp_update_bits64);
+
+/* This is for registers bits with attribute RWC */
+void snd_sof_dsp_update_bits_forced(struct snd_sof_dev *sdev, u32 bar,
+				    u32 offset, u32 mask, u32 value)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&sdev->hw_lock, flags);
+	snd_sof_dsp_update_bits_forced_unlocked(sdev, bar, offset, mask, value);
+	spin_unlock_irqrestore(&sdev->hw_lock, flags);
+}
+EXPORT_SYMBOL(snd_sof_dsp_update_bits_forced);
+
+int snd_sof_dsp_register_poll(struct snd_sof_dev *sdev, u32 bar, u32 offset,
+			      u32 mask, u32 target, u32 timeout)
+{
+	int time, ret;
+
+	/*
+	 * we will poll for couple of ms using mdelay, if not successful
+	 * then go to longer sleep using usleep_range
+	 */
+
+	/* check if set state successful */
+	for (time = 5; time > 0; time--) {
+		if ((snd_sof_dsp_read(sdev, bar, offset) & mask) == target)
+			break;
+		msleep(20);
+	}
+
+	if (!time) {
+		/* sleeping in 10ms steps so adjust timeout value */
+		timeout /= 10;
+
+		for (time = timeout; time > 0; time--) {
+			if ((snd_sof_dsp_read(sdev, bar, offset) & mask) ==
+				target)
+				break;
+
+			usleep_range(5000, 10000);
+		}
+	}
+
+	ret = time ? 0 : -ETIME;
+
+	return ret;
+}
+EXPORT_SYMBOL(snd_sof_dsp_register_poll);
+
+void snd_sof_dsp_panic(struct snd_sof_dev *sdev, u32 offset)
+{
+	dev_err(sdev->dev, "error : DSP panic!\n");
+
+	/* check if DSP is not ready and did not set the dsp_oops_offset.
+	 * if the dsp_oops_offset is not set, set it from the panic message.
+	 * Also add a check to memory window setting with panic message.
+	 */
+	if (!sdev->dsp_oops_offset)
+		sdev->dsp_oops_offset = offset;
+	else
+		dev_dbg(sdev->dev, "panic: dsp_oops_offset %zu offset %d\n",
+			sdev->dsp_oops_offset, offset);
+
+	snd_sof_dsp_dbg_dump(sdev, SOF_DBG_REGS | SOF_DBG_MBOX);
+	snd_sof_trace_notify_for_error(sdev);
+	snd_sof_dsp_cmd_done(sdev, SOF_IPC_HOST_REPLY);
+}
+EXPORT_SYMBOL(snd_sof_dsp_panic);
sound/soc/sof/ops.h new file +379 -0

379 lines not on this page · raw message

Signature · 1 line
2.17.1
#66 lore raw

Re: [PATCH v3 08/14] ASoC: SOF: Add DSP HW abstraction operations

Quoted 19 lines · Pierre-Louis Bossart

From: Liam Girdwood <[email protected]>

Add operation pointers that can be called by core to control a wide variety of DSP targets. The DSP HW drivers will fill in these operations.

sound/soc/sof/ops.c new file +13 -0
+int snd_sof_pci_update_bits_unlocked(struct snd_sof_dev *sdev, u32 offset,
+				     u32 mask, u32 value)
+{
+	bool change;
+	unsigned int old, new;
+	u32 ret = ~0; /* explicit init to remove uninitialized use warnings */
+
+	pci_read_config_dword(sdev->pci, offset, &ret);
+	dev_dbg(sdev->dev, "Debug PCIR: %8.8x at  %8.8x\n",
+		pci_read_config_dword(sdev->pci, offset, &ret), offset);
+
+	old = ret;
+	new = (old & (~mask)) | (value & mask);

sound/soc/sof/ops.c line 35

Quoted 10 lines
sound/soc/sof/ops.c new file +9 -0
+
 
+	change = (old != new);
+	if (change) {
+		pci_write_config_dword(sdev->pci, offset, new);
+		dev_dbg(sdev->dev, "Debug PCIW: %8.8x at  %8.8x\n", value,
+			offset);
+	}
+
+	return change;

What about dropping change completely?

if (old == new)

	return false;

...
return true;

Quoted 10 lines
sound/soc/sof/ops.c new file +9 -0
+}
+EXPORT_SYMBOL(snd_sof_pci_update_bits_unlocked);
 
+int snd_sof_dsp_update_bits_unlocked(struct snd_sof_dev *sdev, u32 bar,
+				     u32 offset, u32 mask, u32 value)
+{
+	bool change;
+	unsigned int old, new;
+	u32 ret;
+

sound/soc/sof/ops.c line 68

Quoted 10 lines
sound/soc/sof/ops.c new file +10 -0
+	ret = snd_sof_dsp_read(sdev, bar, offset);
+
+	old = ret;
+	new = (old & (~mask)) | (value & mask);
+
+	change = (old != new);
+	if (change)
+		snd_sof_dsp_write(sdev, bar, offset, new);
+
+	return change;

Ditto.
Everywhere in similar places.

Quoted 8 lines
sound/soc/sof/ops.c new file +7 -0
+}
+EXPORT_SYMBOL(snd_sof_dsp_update_bits_unlocked);
 
+	/* check if set state successful */
+	for (time = 5; time > 0; time--) {
+		if ((snd_sof_dsp_read(sdev, bar, offset) & mask) == target)
+			break;
+		msleep(20);

sound/soc/sof/ops.c line 169

Quoted 10 lines
sound/soc/sof/ops.c new file +9 -0
+	}
+
+	if (!time) {
+		/* sleeping in 10ms steps so adjust timeout value */
+		timeout /= 10;
+
+		for (time = timeout; time > 0; time--) {
 
+			if ((snd_sof_dsp_read(sdev, bar, offset) & mask) ==
+				target)

I would leave it on one line.

Quoted 9 lines
sound/soc/sof/ops.c new file +9 -0
+				break;
+
+			usleep_range(5000, 10000);
+		}
+	}
+
+	ret = time ? 0 : -ETIME;
+
+	return ret;

sound/soc/sof/ops.c

Quoted 10 lines
sound/soc/sof/ops.c new file +8 -0
+}
+EXPORT_SYMBOL(snd_sof_dsp_register_poll);
 
+static inline void snd_sof_dsp_write64(struct snd_sof_dev *sdev, u32 bar,
+				       u32 offset, u64 value)
+{
+	if (sdev->ops->write64)
 
+		sdev->ops->write64(sdev,
+			sdev->bar[bar] + offset, value);

Why not one line?

Quoted 1 line

+}

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#67 lore raw

Re: [PATCH v3 08/14] ASoC: SOF: Add DSP HW abstraction operations

Quoted 30 lines · Andy Shevchenko
Quoted 17 lines · Pierre-Louis Bossart

From: Liam Girdwood <[email protected]>

Add operation pointers that can be called by core to control a wide variety of DSP targets. The DSP HW drivers will fill in these operations.

sound/soc/sof/ops.c new file +12 -0
+int snd_sof_pci_update_bits_unlocked(struct snd_sof_dev *sdev, u32 offset,
+				     u32 mask, u32 value)
+{
+	bool change;
+	unsigned int old, new;
+	u32 ret = ~0; /* explicit init to remove uninitialized use warnings */
+
+	pci_read_config_dword(sdev->pci, offset, &ret);
+	dev_dbg(sdev->dev, "Debug PCIR: %8.8x at  %8.8x\n",
+		pci_read_config_dword(sdev->pci, offset, &ret), offset);
+
+	old = ret;

sound/soc/sof/ops.c line 35

Quoted 10 lines
sound/soc/sof/ops.c new file +10 -0
+	new = (old & (~mask)) | (value & mask);
+
+	change = (old != new);
+	if (change) {
+		pci_write_config_dword(sdev->pci, offset, new);
+		dev_dbg(sdev->dev, "Debug PCIW: %8.8x at  %8.8x\n", value,
+			offset);
+	}
+
+	return change;

What about dropping change completely?

Quoted 5 lines

if (old == new)

	return false;

...
return true;

ok for all cases

Quoted 33 lines
Quoted 11 lines
sound/soc/sof/ops.c new file +9 -0
+}
+EXPORT_SYMBOL(snd_sof_pci_update_bits_unlocked);
+int snd_sof_dsp_update_bits_unlocked(struct snd_sof_dev *sdev, u32 bar,
+				     u32 offset, u32 mask, u32 value)
+{
+	bool change;
+	unsigned int old, new;
+	u32 ret;
+

sound/soc/sof/ops.c line 68

Quoted 10 lines
sound/soc/sof/ops.c new file +10 -0
+	ret = snd_sof_dsp_read(sdev, bar, offset);
+
+	old = ret;
+	new = (old & (~mask)) | (value & mask);
+
+	change = (old != new);
+	if (change)
+		snd_sof_dsp_write(sdev, bar, offset, new);
+
+	return change;

Ditto.
Everywhere in similar places.

Quoted 9 lines
sound/soc/sof/ops.c new file +9 -0
+}
+EXPORT_SYMBOL(snd_sof_dsp_update_bits_unlocked);
+	/* check if set state successful */
+	for (time = 5; time > 0; time--) {
+		if ((snd_sof_dsp_read(sdev, bar, offset) & mask) == target)
+			break;
+		msleep(20);
+	}
+
Quoted 8 lines

sound/soc/sof/ops.c line 169

Quoted 7 lines
sound/soc/sof/ops.c new file +7 -0
+	if (!time) {
+		/* sleeping in 10ms steps so adjust timeout value */
+		timeout /= 10;
+
+		for (time = timeout; time > 0; time--) {
+			if ((snd_sof_dsp_read(sdev, bar, offset) & mask) ==
+				target)

I would leave it on one line.

ok

Quoted 11 lines
Quoted 11 lines
sound/soc/sof/ops.c new file +10 -0
+				break;
+
+			usleep_range(5000, 10000);
+		}
+	}
+
+	ret = time ? 0 : -ETIME;
+
+	return ret;
+}
Quoted 8 lines

sound/soc/sof/ops.h line 161

Quoted 7 lines
sound/soc/sof/ops.h new file +7 -0
+EXPORT_SYMBOL(snd_sof_dsp_register_poll);
+static inline void snd_sof_dsp_write64(struct snd_sof_dev *sdev, u32 bar,
+				       u32 offset, u64 value)
+{
+	if (sdev->ops->write64)
+		sdev->ops->write64(sdev,
+			sdev->bar[bar] + offset, value);

Why not one line?

ok

Quoted 3 lines
Quoted 2 lines

+}

maintainer
#68 lore raw

Re: [PATCH v3 08/14] ASoC: SOF: Add DSP HW abstraction operations

In reply to Pierre-Louis Bossart

sound/soc/sof/ops.c line 19

Quoted 6 lines · Pierre-Louis Bossart
sound/soc/sof/ops.c new file +6 -0
+int snd_sof_pci_update_bits_unlocked(struct snd_sof_dev *sdev, u32 offset,
+				     u32 mask, u32 value)
+{
+	bool change;
+	unsigned int old, new;
+	u32 ret = ~0; /* explicit init to remove uninitialized use warnings */

This looks a lot like you want to write regmap_pci_config...

sound/soc/sof/ops.h line 45

Quoted 8 lines
sound/soc/sof/ops.h new file +8 -0
+/* control */
+static inline int snd_sof_dsp_run(struct snd_sof_dev *sdev)
+{
+	if (sdev->ops->run)
+		return sdev->ops->run(sdev);
+
+	return 0;
+}

Do we really want to return 0 for all these ops if they're not implemented? For some that seems sensible but there's others where it seems like the caller might want to know they got ignored and an error code like -ENOTSUPP might be better.

#69 lore raw

Re: [PATCH v3 08/14] ASoC: SOF: Add DSP HW abstraction operations

Quoted 9 lines · Mark Brown

sound/soc/sof/ops.c line 19

Quoted 6 lines · Pierre-Louis Bossart
sound/soc/sof/ops.c new file +6 -0
+int snd_sof_pci_update_bits_unlocked(struct snd_sof_dev *sdev, u32 offset,
+				     u32 mask, u32 value)
+{
+	bool change;
+	unsigned int old, new;
+	u32 ret = ~0; /* explicit init to remove uninitialized use warnings */

This looks a lot like you want to write regmap_pci_config...

I think you made that note for the v2 a long time ago, not sure if I replied at the time.

We only use this for 4 cases (power/clock gating on/off, traffic class, etc) and only during the hardware initialization. This is similar to the legacy and Skylake driver, I don't see a significant benefit with a regmap?

intel/hda-ctrl.c:       snd_sof_pci_update_bits(sdev, PCI_CGCTL, PCI_CGCTL_MISCBDCGE_MASK, val); intel/hda-ctrl.c:       snd_sof_pci_update_bits(sdev, PCI_CGCTL, PCI_CGCTL_ADSPDCGE, val); intel/hda-ctrl.c:       snd_sof_pci_update_bits(sdev, PCI_PGCTL, PCI_PGCTL_ADSPPGD, val); intel/hda-dsp.c:        snd_sof_pci_update_bits(sdev, PCI_PGCTL, intel/hda-dsp.c:        snd_sof_pci_update_bits(sdev, PCI_TCSEL, 0x07, 0);

Quoted 13 lines

sound/soc/sof/ops.h line 45

Quoted 9 lines
sound/soc/sof/ops.h new file +8 -0
+/* control */
+static inline int snd_sof_dsp_run(struct snd_sof_dev *sdev)
+{
+	if (sdev->ops->run)
+		return sdev->ops->run(sdev);
+
+	return 0;
+}

Do we really want to return 0 for all these ops if they're not implemented? For some that seems sensible but there's others where it seems like the caller might want to know they got ignored and an error code like -ENOTSUPP might be better.

Good point indeed. There are a set of ops that are really mandatory, we should rework this instead. Thanks for the comment.

maintainer
#70 lore raw

Re: [PATCH v3 08/14] ASoC: SOF: Add DSP HW abstraction operations

Quoted 12 lines · Pierre-Louis Bossart
Quoted 10 lines · Mark Brown

sound/soc/sof/ops.c line 19

Quoted 6 lines · Pierre-Louis Bossart
sound/soc/sof/ops.c new file +6 -0
+int snd_sof_pci_update_bits_unlocked(struct snd_sof_dev *sdev, u32 offset,
+				     u32 mask, u32 value)
+{
+	bool change;
+	unsigned int old, new;
+	u32 ret = ~0; /* explicit init to remove uninitialized use warnings */

This looks a lot like you want to write regmap_pci_config...

Quoted 6 lines

I think you made that note for the v2 a long time ago, not sure if I replied at the time.

We only use this for 4 cases (power/clock gating on/off, traffic class, etc) and only during the hardware initialization. This is similar to the legacy and Skylake driver, I don't see a significant benefit with a regmap?

Mainly the debugfs stuff and trace I'd expect. It's not a requirement.

#71 lore raw

PATCH v3 9/14 ASoC: SOF: Add firmware loader support

In reply to Pierre-Louis Bossart

From: Liam Girdwood <[email protected]>

The firmware loader exports APIs that can be called by core to load and process multiple different file formats.

---
 sound/soc/sof/loader.c | 322 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 322 insertions(+)
 create mode 100644 sound/soc/sof/loader.c
sound/soc/sof/loader.c new file +322 -0
new file mode 100644
index 000000000000..dbadd8c521e6
@@ -0,0 +1,322 @@
+// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
+//
+// This file is provided under a dual BSD/GPLv2 license.  When using or
+// redistributing this file, you may do so under either license.
+//
+// Copyright(c) 2018 Intel Corporation. All rights reserved.
+//
+// Author: Liam Girdwood <[email protected]>
+//
+// Generic firmware loader.
+//
+
+#include <linux/firmware.h>
+#include <sound/sof.h>
+#include "ops.h"
+
+static int get_ext_windows(struct snd_sof_dev *sdev,
+			   struct sof_ipc_ext_data_hdr *ext_hdr)
+{
+	struct sof_ipc_window *w = (struct sof_ipc_window *)ext_hdr;
+
+	int ret = 0;
+	size_t size;
+
+	if (w->num_windows == 0 || w->num_windows > SOF_IPC_MAX_ELEMS)
+		return -EINVAL;
+
+	size = sizeof(*w) + sizeof(struct sof_ipc_window_elem) * w->num_windows;
+
+	/* keep a local copy of the data */
+	sdev->info_window = kmemdup(w, size, GFP_KERNEL);
+	if (!sdev->info_window)
+		return -ENOMEM;
+
+	return ret;
+}
+
+/* parse the extended FW boot data structures from FW boot message */
+int snd_sof_fw_parse_ext_data(struct snd_sof_dev *sdev, u32 offset)
+{
+	struct sof_ipc_ext_data_hdr *ext_hdr;
+	void *ext_data;
+	int ret = 0;
+
+	ext_data = kzalloc(PAGE_SIZE, GFP_KERNEL);
+	if (!ext_data)
+		return -ENOMEM;
+
+	/* get first header */
+	snd_sof_dsp_block_read(sdev, offset, ext_data, sizeof(*ext_hdr));
+	ext_hdr = (struct sof_ipc_ext_data_hdr *)ext_data;
+
+	while (ext_hdr->hdr.cmd == SOF_IPC_FW_READY) {
+		/* read in ext structure */
+		offset += sizeof(*ext_hdr);
+		snd_sof_dsp_block_read(sdev, offset,
+				       ext_data + sizeof(*ext_hdr),
+				       ext_hdr->hdr.size - sizeof(*ext_hdr));
+
+		dev_dbg(sdev->dev, "found ext header type %d size 0x%x\n",
+			ext_hdr->type, ext_hdr->hdr.size);
+
+		/* process structure data */
+		switch (ext_hdr->type) {
+		case SOF_IPC_EXT_DMA_BUFFER:
+			break;
+		case SOF_IPC_EXT_WINDOW:
+			ret = get_ext_windows(sdev, ext_hdr);
+			break;
+		default:
+			break;
+		}
+
+		if (ret < 0) {
+			dev_err(sdev->dev, "error: failed to parse ext data type %d\n",
+				ext_hdr->type);
+			goto out;
+		}
+
+		/* move to next header */
+		offset += ext_hdr->hdr.size;
+		snd_sof_dsp_block_read(sdev, offset, ext_data,
+				       sizeof(*ext_hdr));
+		ext_hdr = (struct sof_ipc_ext_data_hdr *)ext_data;
+	}
+out:
+	kfree(ext_data);
+	return ret;
+}
+EXPORT_SYMBOL(snd_sof_fw_parse_ext_data);
+
+/* generic module parser for mmaped DSPs */
+int snd_sof_parse_module_memcpy(struct snd_sof_dev *sdev,
+				struct snd_sof_mod_hdr *module)
+{
+	struct snd_sof_blk_hdr *block;
+	int count;
+	u32 offset;
+
+	dev_dbg(sdev->dev, "new module size 0x%x blocks 0x%x type 0x%x\n",
+		module->size, module->num_blocks, module->type);
+
+	block = (void *)module + sizeof(*module);
+
+	for (count = 0; count < module->num_blocks; count++) {
+		if (block->size == 0) {
+			dev_warn(sdev->dev,
+				 "warning: block %d size zero\n", count);
+			dev_warn(sdev->dev, " type 0x%x offset 0x%x\n",
+				 block->type, block->offset);
+			continue;
+		}
+
+		switch (block->type) {
+		case SOF_BLK_IMAGE:
+		case SOF_BLK_CACHE:
+		case SOF_BLK_REGS:
+		case SOF_BLK_SIG:
+		case SOF_BLK_ROM:
+			continue;	/* not handled atm */
+		case SOF_BLK_TEXT:
+		case SOF_BLK_DATA:
+			offset = block->offset;
+			break;
+		default:
+			dev_err(sdev->dev, "error: bad type 0x%x for block 0x%x\n",
+				block->type, count);
+			return -EINVAL;
+		}
+
+		dev_dbg(sdev->dev,
+			"block %d type 0x%x size 0x%x ==>  offset 0x%x\n",
+			count, block->type, block->size, offset);
+
+		snd_sof_dsp_block_write(sdev, offset,
+					(void *)block + sizeof(*block),
+					block->size);
+
+		/* next block */
+		block = (void *)block + sizeof(*block) + block->size;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(snd_sof_parse_module_memcpy);
+
+static int check_header(struct snd_sof_dev *sdev, const struct firmware *fw)
+{
+	struct snd_sof_fw_header *header;
+
+	/* Read the header information from the data pointer */
+	header = (struct snd_sof_fw_header *)fw->data;
+
+	/* verify FW sig */
+	if (strncmp(header->sig, SND_SOF_FW_SIG, SND_SOF_FW_SIG_SIZE) != 0) {
+		dev_err(sdev->dev, "error: invalid firmware signature\n");
+		return -EINVAL;
+	}
+
+	/* check size is valid */
+	if (fw->size != header->file_size + sizeof(*header)) {
+		dev_err(sdev->dev, "error: invalid filesize mismatch got 0x%zx expected 0x%zx\n",
+			fw->size, header->file_size + sizeof(*header));
+		return -EINVAL;
+	}
+
+	dev_dbg(sdev->dev, "header size=0x%x modules=0x%x abi=0x%x size=%zu\n",
+		header->file_size, header->num_modules,
+		header->abi, sizeof(*header));
+
+	return 0;
+}
+
+static int load_modules(struct snd_sof_dev *sdev, const struct firmware *fw)
+{
+	struct snd_sof_fw_header *header;
+	struct snd_sof_mod_hdr *module;
+	int (*load_module)(struct snd_sof_dev *sof_dev,
+			   struct snd_sof_mod_hdr *hdr);
+	int ret, count;
+
+	header = (struct snd_sof_fw_header *)fw->data;
+	load_module = sdev->ops->load_module;
+	if (!load_module)
+		return -EINVAL;
+
+	/* parse each module */
+	module = (void *)fw->data + sizeof(*header);
+	for (count = 0; count < header->num_modules; count++) {
+		/* module */
+		ret = load_module(sdev, module);
+		if (ret < 0) {
+			dev_err(sdev->dev, "error: invalid module %d\n", count);
+			return ret;
+		}
+		module = (void *)module + sizeof(*module) + module->size;
+	}
+
+	return 0;
+}
+
+int snd_sof_load_firmware_memcpy(struct snd_sof_dev *sdev)
+{
+	struct snd_sof_pdata *plat_data = dev_get_platdata(sdev->dev);
+	const char *fw_filename;
+	int ret;
+
+	/* set code loading condition to true */
+	sdev->code_loading = 1;
+	fw_filename = plat_data->machine->sof_fw_filename;
+
+	ret = request_firmware(&plat_data->fw, fw_filename, sdev->dev);
+
+	if (ret < 0) {
+		dev_err(sdev->dev, "error: request firmware failed err: %d\n",
+			ret);
+		return ret;
+	}
+
+	/* make sure the FW header and file is valid */
+	ret = check_header(sdev, plat_data->fw);
+	if (ret < 0) {
+		dev_err(sdev->dev, "error: invalid FW header\n");
+		goto error;
+	}
+
+	/* prepare the DSP for FW loading */
+	ret = snd_sof_dsp_reset(sdev);
+	if (ret < 0) {
+		dev_err(sdev->dev, "error: failed to reset DSP\n");
+		goto error;
+	}
+
+	/* parse and load firmware modules to DSP */
+	ret = load_modules(sdev, plat_data->fw);
+	if (ret < 0) {
+		dev_err(sdev->dev, "error: invalid FW modules\n");
+		goto error;
+	}
+
+	return 0;
+
+error:
+	release_firmware(plat_data->fw);
+	return ret;
+
+}
+EXPORT_SYMBOL(snd_sof_load_firmware_memcpy);
+
+int snd_sof_load_firmware(struct snd_sof_dev *sdev)
+{
+	dev_dbg(sdev->dev, "loading firmware\n");
+
+	if (sdev->ops->load_firmware)
+		return sdev->ops->load_firmware(sdev);
+	return 0;
+}
+EXPORT_SYMBOL(snd_sof_load_firmware);
+
+int snd_sof_run_firmware(struct snd_sof_dev *sdev)
+{
+	int ret;
+
+	init_waitqueue_head(&sdev->boot_wait);
+	sdev->boot_complete = false;
+
+	/* create fw_version debugfs to store boot version info */
+	if (sdev->first_boot) {
+		ret = snd_sof_debugfs_buf_create_item(sdev, &sdev->fw_version,
+						      sizeof(sdev->fw_version),
+						      "fw_version");
+
+		if (ret < 0) {
+			dev_err(sdev->dev, "error: cannot create debugfs for fw_version\n");
+			return ret;
+		}
+	}
+
+	/* perform pre fw run operations */
+	ret = snd_sof_dsp_pre_fw_run(sdev);
+	if (ret < 0) {
+		dev_err(sdev->dev, "error: failed pre fw run op\n");
+		return ret;
+	}
+
+	dev_dbg(sdev->dev, "booting DSP firmware\n");
+
+	/* boot the firmware on the DSP */
+	ret = snd_sof_dsp_run(sdev);
+	if (ret < 0) {
+		dev_err(sdev->dev, "error: failed to reset DSP\n");
+		return ret;
+	}
+
+	/* now wait for the DSP to boot */
+	ret = wait_event_timeout(sdev->boot_wait, sdev->boot_complete,
+				 msecs_to_jiffies(sdev->boot_timeout));
+	if (ret == 0) {
+		dev_err(sdev->dev, "error: firmware boot timeout\n");
+		snd_sof_dsp_dbg_dump(sdev, SOF_DBG_REGS | SOF_DBG_MBOX |
+			SOF_DBG_TEXT | SOF_DBG_PCI);
+		return -EIO;
+	}
+
+	dev_info(sdev->dev, "firmware boot complete\n");
+
+	/* perform post fw run operations */
+	ret = snd_sof_dsp_post_fw_run(sdev);
+	if (ret < 0) {
+		dev_err(sdev->dev, "error: failed post fw run op\n");
+		return ret;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(snd_sof_run_firmware);
+
+void snd_sof_fw_unload(struct snd_sof_dev *sdev)
+{
+	/* TODO: support module unloading at runtime */
+}
+EXPORT_SYMBOL(snd_sof_fw_unload);
Signature · 1 line
2.17.1
#72 lore raw

Re: [PATCH v3 09/14] ASoC: SOF: Add firmware loader support

sound/soc/sof/loader.c line 22

Quoted 12 lines · Pierre-Louis Bossart

From: Liam Girdwood <[email protected]>

The firmware loader exports APIs that can be called by core to load and process multiple different file formats.

sound/soc/sof/loader.c new file +6 -0
+static int get_ext_windows(struct snd_sof_dev *sdev,
+			   struct sof_ipc_ext_data_hdr *ext_hdr)
+{
+	struct sof_ipc_window *w = (struct sof_ipc_window *)ext_hdr;
+
 
+	int ret = 0;

I don't see how it's used. Perhaps you need to check code with make W=1.

Quoted 9 lines
sound/soc/sof/loader.c new file +9 -0
+	size_t size;
+
+	if (w->num_windows == 0 || w->num_windows > SOF_IPC_MAX_ELEMS)
+		return -EINVAL;
+
+	size = sizeof(*w) + sizeof(struct sof_ipc_window_elem) * w->num_windows;
+
+	/* keep a local copy of the data */
+	sdev->info_window = kmemdup(w, size, GFP_KERNEL);

sound/soc/sof/loader.c

Quoted 10 lines
sound/soc/sof/loader.c new file +9 -0
+	if (!sdev->info_window)
+		return -ENOMEM;
+
+	return ret;
+}
 
+			dev_warn(sdev->dev,
+				 "warning: block %d size zero\n", count);
+			dev_warn(sdev->dev, " type 0x%x offset 0x%x\n",
+				 block->type, block->offset);

Hmm... Why do we need a kernel level duplication in words?

sound/soc/sof/loader.c line 252

Quoted 4 lines
sound/soc/sof/loader.c new file +3 -0
+int snd_sof_load_firmware(struct snd_sof_dev *sdev)
+{
 
+	dev_dbg(sdev->dev, "loading firmware\n");

Noise.
Better to introduce a trace points and drop all these kind of messages.

Quoted 6 lines
sound/soc/sof/loader.c new file +6 -0
+
+	if (sdev->ops->load_firmware)
+		return sdev->ops->load_firmware(sdev);
+	return 0;
+}
+EXPORT_SYMBOL(snd_sof_load_firmware);
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#73 lore raw

Re: [PATCH v3 09/14] ASoC: SOF: Add firmware loader support

Quoted 12 lines · Andy Shevchenko

sound/soc/sof/loader.c line 22

Quoted 10 lines · Pierre-Louis Bossart

From: Liam Girdwood <[email protected]>

The firmware loader exports APIs that can be called by core to load and process multiple different file formats.

sound/soc/sof/loader.c new file +6 -0
+static int get_ext_windows(struct snd_sof_dev *sdev,
+			   struct sof_ipc_ext_data_hdr *ext_hdr)
+{
+	struct sof_ipc_window *w = (struct sof_ipc_window *)ext_hdr;
+
+	int ret = 0;

I don't see how it's used. Perhaps you need to check code with make W=1.

Darn, we missed this one. I thought we were using W=1 on github but we aren't, this will be fixed.

Though W=1 doesn't report this one, so need to re-inspect this. Thanks for the sighting.

Quoted 12 lines
Quoted 12 lines
sound/soc/sof/loader.c new file +11 -0
+	size_t size;
+
+	if (w->num_windows == 0 || w->num_windows > SOF_IPC_MAX_ELEMS)
+		return -EINVAL;
+
+	size = sizeof(*w) + sizeof(struct sof_ipc_window_elem) * w->num_windows;
+
+	/* keep a local copy of the data */
+	sdev->info_window = kmemdup(w, size, GFP_KERNEL);
+	if (!sdev->info_window)
+		return -ENOMEM;
Quoted 8 lines

sound/soc/sof/loader.c line 110

Quoted 7 lines
sound/soc/sof/loader.c new file +7 -0
+
+	return ret;
+}
+			dev_warn(sdev->dev,
+				 "warning: block %d size zero\n", count);
+			dev_warn(sdev->dev, " type 0x%x offset 0x%x\n",
+				 block->type, block->offset);

Hmm... Why do we need a kernel level duplication in words?

Not sure I get this one, it's just a warning message where you don't copy a block of size zero into the target memory.

Quoted 6 lines

sound/soc/sof/loader.c line 252

Quoted 4 lines
sound/soc/sof/loader.c new file +3 -0
+int snd_sof_load_firmware(struct snd_sof_dev *sdev)
+{
+	dev_dbg(sdev->dev, "loading firmware\n");

Noise.
Better to introduce a trace points and drop all these kind of messages.

it's not that bad, most people understand what dmesg is, it happens once and only if you have dynamic debug.

At some point we also thought about something like drm_debug where you can specify which parts you are interested in, in addition to the verbosity level.

Quoted 7 lines
Quoted 7 lines
sound/soc/sof/loader.c new file +6 -0
+
+	if (sdev->ops->load_firmware)
+		return sdev->ops->load_firmware(sdev);
+	return 0;
+}
+EXPORT_SYMBOL(snd_sof_load_firmware);
maintainer
#74 lore raw

Re: [PATCH v3 09/14] ASoC: SOF: Add firmware loader support

Quoted 9 lines · Pierre-Louis Bossart
Quoted 7 lines · Andy Shevchenko

sound/soc/sof/loader.c line 22

Quoted 3 lines · Pierre-Louis Bossart
sound/soc/sof/loader.c new file +3 -0
+	struct sof_ipc_window *w = (struct sof_ipc_window *)ext_hdr;
+
+	int ret = 0;

I don't see how it's used. Perhaps you need to check code with make W=1.

Quoted 5 lines

Darn, we missed this one. I thought we were using W=1 on github but we aren't, this will be fixed.

Though W=1 doesn't report this one, so need to re-inspect this. Thanks for the sighting.

This is one reason not to unconditionally init stuff on declaration - it masks some warnings.

Quoted 8 lines
Quoted 5 lines

sound/soc/sof/loader.c line 252

Quoted 3 lines
sound/soc/sof/loader.c new file +3 -0
+int snd_sof_load_firmware(struct snd_sof_dev *sdev)
+{
+	dev_dbg(sdev->dev, "loading firmware\n");

Noise.
Better to introduce a trace points and drop all these kind of messages.

it's not that bad, most people understand what dmesg is, it happens once and only if you have dynamic debug.

Then everyone adds their print on boot and the log gets huge (and slow if you push debug through serial during development). As a rule of thumb I tend to suggest that if you're not reporting something you discovered at runtime there's probably already other ways of getting equivalent trace.

#75 lore raw

Re: [PATCH v3 09/14] ASoC: SOF: Add firmware loader support

In reply to Pierre-Louis Bossart
Quoted 5 lines · Pierre-Louis Bossart
sound/soc/sof/loader.c new file +4 -0
+/* generic module parser for mmaped DSPs */
+int snd_sof_parse_module_memcpy(struct snd_sof_dev *sdev,
+				struct snd_sof_mod_hdr *module)
+{

sound/soc/sof/loader.c line 105

Quoted 10 lines
sound/soc/sof/loader.c new file +10 -0
+	struct snd_sof_blk_hdr *block;
+	int count;
+	u32 offset;
+
+	dev_dbg(sdev->dev, "new module size 0x%x blocks 0x%x type 0x%x\n",
+		module->size, module->num_blocks, module->type);
+
+	block = (void *)module + sizeof(*module);
+
+	for (count = 0; count < module->num_blocks; count++) {

Need a sanity check that it won't go beyond the actual firmware size. User may pass a malicious module data, e.g. with extra large num_blocks.

Quoted 25 lines
sound/soc/sof/loader.c new file +25 -0
+		if (block->size == 0) {
+			dev_warn(sdev->dev,
+				 "warning: block %d size zero\n", count);
+			dev_warn(sdev->dev, " type 0x%x offset 0x%x\n",
+				 block->type, block->offset);
+			continue;
+		}
+
+		switch (block->type) {
+		case SOF_BLK_IMAGE:
+		case SOF_BLK_CACHE:
+		case SOF_BLK_REGS:
+		case SOF_BLK_SIG:
+		case SOF_BLK_ROM:
+			continue;	/* not handled atm */
+		case SOF_BLK_TEXT:
+		case SOF_BLK_DATA:
+			offset = block->offset;
+			break;
+		default:
+			dev_err(sdev->dev, "error: bad type 0x%x for block 0x%x\n",
+				block->type, count);
+			return -EINVAL;
+		}
+

sound/soc/sof/loader.c line 140

Quoted 10 lines
sound/soc/sof/loader.c new file +10 -0
+		dev_dbg(sdev->dev,
+			"block %d type 0x%x size 0x%x ==>  offset 0x%x\n",
+			count, block->type, block->size, offset);
+
+		snd_sof_dsp_block_write(sdev, offset,
+					(void *)block + sizeof(*block),
+					block->size);
+
+		/* next block */
+		block = (void *)block + sizeof(*block) + block->size;

This may lead to an unaligned access.
Also how is the endianess guaranteed?

thanks,

Takashi

#76 lore raw

Re: [Sound-open-firmware] [PATCH v3 09/14] ASoC: SOF: Add firmware loader support

Quoted 11 lines · Takashi Iwai
Quoted 9 lines · Pierre-Louis Bossart
sound/soc/sof/loader.c new file +9 -0
+/* generic module parser for mmaped DSPs */
+int snd_sof_parse_module_memcpy(struct snd_sof_dev *sdev,
+				struct snd_sof_mod_hdr *module)
+{
+	struct snd_sof_blk_hdr *block;
+	int count;
+	u32 offset;
+
+	dev_dbg(sdev->dev, "new module size 0x%x blocks 0x%x type 0x%x\n",
Quoted 8 lines

sound/soc/sof/loader.c line 105

Quoted 5 lines
sound/soc/sof/loader.c new file +5 -0
+		module->size, module->num_blocks, module->type);
+
+	block = (void *)module + sizeof(*module);
+
+	for (count = 0; count < module->num_blocks; count++) {

Need a sanity check that it won't go beyond the actual firmware size. User may pass a malicious module data, e.g. with extra large num_blocks.

Good point, will check.

Quoted 30 lines
Quoted 30 lines
sound/soc/sof/loader.c new file +29 -0
+		if (block->size == 0) {
+			dev_warn(sdev->dev,
+				 "warning: block %d size zero\n", count);
+			dev_warn(sdev->dev, " type 0x%x offset 0x%x\n",
+				 block->type, block->offset);
+			continue;
+		}
+
+		switch (block->type) {
+		case SOF_BLK_IMAGE:
+		case SOF_BLK_CACHE:
+		case SOF_BLK_REGS:
+		case SOF_BLK_SIG:
+		case SOF_BLK_ROM:
+			continue;	/* not handled atm */
+		case SOF_BLK_TEXT:
+		case SOF_BLK_DATA:
+			offset = block->offset;
+			break;
+		default:
+			dev_err(sdev->dev, "error: bad type 0x%x for block 0x%x\n",
+				block->type, count);
+			return -EINVAL;
+		}
+
+		dev_dbg(sdev->dev,
+			"block %d type 0x%x size 0x%x ==>  offset 0x%x\n",
+			count, block->type, block->size, offset);
+
Quoted 8 lines

sound/soc/sof/loader.c line 140

Quoted 6 lines
sound/soc/sof/loader.c new file +6 -0
+		snd_sof_dsp_block_write(sdev, offset,
+					(void *)block + sizeof(*block),
+					block->size);
+
+		/* next block */
+		block = (void *)block + sizeof(*block) + block->size;

This may lead to an unaligned access.
Also how is the endianess guaranteed?

Will check, valid points.

maintainer
#77 lore raw

Re: [PATCH v3 09/14] ASoC: SOF: Add firmware loader support

In reply to Pierre-Louis Bossart

sound/soc/sof/loader.c line 206

Quoted 5 lines · Pierre-Louis Bossart
sound/soc/sof/loader.c new file +5 -0
+int snd_sof_load_firmware_memcpy(struct snd_sof_dev *sdev)
+{
+	struct snd_sof_pdata *plat_data = dev_get_platdata(sdev->dev);
+	const char *fw_filename;
+	int ret;

This never actually calls the load_firmware() operation for the DSP AFAICT?

sound/soc/sof/loader.c line 218

Quoted 7 lines
sound/soc/sof/loader.c new file +7 -0
+	ret = request_firmware(&plat_data->fw, fw_filename, sdev->dev);
+
+	if (ret < 0) {
+		dev_err(sdev->dev, "error: request firmware failed err: %d\n",
+			ret);
+		return ret;
+	}

I'd suggest logging the name of the firmware we tried to load, users will thank you.

sound/soc/sof/loader.c line 277

Quoted 11 lines
sound/soc/sof/loader.c new file +11 -0
+	/* create fw_version debugfs to store boot version info */
+	if (sdev->first_boot) {
+		ret = snd_sof_debugfs_buf_create_item(sdev, &sdev->fw_version,
+						      sizeof(sdev->fw_version),
+						      "fw_version");
+
+		if (ret < 0) {
+			dev_err(sdev->dev, "error: cannot create debugfs for fw_version\n");
+			return ret;
+		}
+	}

As Andy said elsewhere debugfs stuff like that probably shouldn't be fatal.

#78 lore raw

Re: [PATCH v3 09/14] ASoC: SOF: Add firmware loader support

Thanks for the reviews Mark, much appreciated.

+int snd_sof_load_firmware_memcpy(struct snd_sof_dev *sdev)

Quoted 6 lines

sound/soc/sof/loader.c line 206

Quoted 4 lines
sound/soc/sof/loader.c new file +4 -0
+{
+	struct snd_sof_pdata *plat_data = dev_get_platdata(sdev->dev);
+	const char *fw_filename;
+	int ret;

This never actually calls the load_firmware() operation for the DSP AFAICT?

it's actually the implementation of the load_firmware callback, see e.g. for Baytrail/Broadwell

/*Firmware loading */
    .load_firmware    = snd_sof_load_firmware_memcpy,

Quoted 10 lines

sound/soc/sof/loader.c line 218

Quoted 8 lines
sound/soc/sof/loader.c new file +7 -0
+	ret = request_firmware(&plat_data->fw, fw_filename, sdev->dev);
+
+	if (ret < 0) {
+		dev_err(sdev->dev, "error: request firmware failed err: %d\n",
+			ret);
+		return ret;
+	}

I'd suggest logging the name of the firmware we tried to load, users will thank you.

yes indeed, thanks for the suggestion, will add this right away.

Quoted 14 lines

sound/soc/sof/loader.c line 277

Quoted 12 lines
sound/soc/sof/loader.c new file +11 -0
+	/* create fw_version debugfs to store boot version info */
+	if (sdev->first_boot) {
+		ret = snd_sof_debugfs_buf_create_item(sdev, &sdev->fw_version,
+						      sizeof(sdev->fw_version),
+						      "fw_version");
+
+		if (ret < 0) {
+			dev_err(sdev->dev, "error: cannot create debugfs for fw_version\n");
+			return ret;
+		}
+	}

As Andy said elsewhere debugfs stuff like that probably shouldn't be fatal.

yes, we've fixed this already.

#79 lore raw

PATCH v3 10/14 ASoC: SOF: Add userspace ABI support

In reply to Pierre-Louis Bossart

From: Liam Girdwood <[email protected]>

Add userspace ABI for audio userspace application IO outside of regular ALSA PCM and kcontrols. This is intended to be used to format coefficients and data for custom processing components.

---
 include/uapi/sound/sof/abi.h      |  63 ++++++++++
 include/uapi/sound/sof/eq.h       | 164 ++++++++++++++++++++++++++
 include/uapi/sound/sof/fw.h       |  67 +++++++++++
 include/uapi/sound/sof/header.h   |  27 +++++
 include/uapi/sound/sof/manifest.h | 188 ++++++++++++++++++++++++++++++
 include/uapi/sound/sof/tokens.h   |  95 +++++++++++++++
 include/uapi/sound/sof/tone.h     |  21 ++++
 include/uapi/sound/sof/trace.h    |  93 +++++++++++++++
 8 files changed, 718 insertions(+)
 create mode 100644 include/uapi/sound/sof/abi.h
 create mode 100644 include/uapi/sound/sof/eq.h
 create mode 100644 include/uapi/sound/sof/fw.h
 create mode 100644 include/uapi/sound/sof/header.h
 create mode 100644 include/uapi/sound/sof/manifest.h
 create mode 100644 include/uapi/sound/sof/tokens.h
 create mode 100644 include/uapi/sound/sof/tone.h
 create mode 100644 include/uapi/sound/sof/trace.h
include/uapi/sound/sof/abi.h new file +63 -0
new file mode 100644
index 000000000000..cc0d3d2a47a7
@@ -0,0 +1,63 @@
+/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) */
+/*
+ * This file is provided under a dual BSD/GPLv2 license.  When using or
+ * redistributing this file, you may do so under either license.
+ *
+ * Copyright(c) 2018 Intel Corporation. All rights reserved.
+ */
+
+/**
+ * SOF ABI versioning is based on Semantic Versioning where we have a given
+ * MAJOR.MINOR.PATCH version number. See https://semver.org/
+ *
+ * Rules for incrementing or changing version :-
+ *
+ * 1) Increment MAJOR version if you make incompatible API changes. MINOR and
+ *    PATCH should be reset to 0.
+ *
+ * 2) Increment MINOR version if you add backwards compatible features or
+ *    changes. PATCH should be reset to 0.
+ *
+ * 3) Increment PATCH version if you add backwards compatible bug fixes.
+ */
+
+#ifndef __INCLUDE_UAPI_SOUND_SOF_ABI_H__
+#define __INCLUDE_UAPI_SOUND_SOF_ABI_H__
+
+/* SOF ABI version major, minor and patch numbers */
+#define SOF_ABI_MAJOR 3
+#define SOF_ABI_MINOR 0
+#define SOF_ABI_PATCH 0
+
+/* SOF ABI version number. Format within 32bit word is MMmmmppp */
+#define SOF_ABI_MAJOR_SHIFT	24
+#define SOF_ABI_MAJOR_MASK	0xff
+#define SOF_ABI_MINOR_SHIFT	12
+#define SOF_ABI_MINOR_MASK	0xfff
+#define SOF_ABI_PATCH_SHIFT	0
+#define SOF_ABI_PATCH_MASK	0xfff
+
+#define SOF_ABI_VER(major, minor, patch) \
+	(((major) << SOF_ABI_MAJOR_SHIFT) | \
+	((minor) << SOF_ABI_MINOR_SHIFT) | \
+	((patch) << SOF_ABI_PATCH_SHIFT))
+
+#define SOF_ABI_VERSION_MAJOR(version) \
+	(((version) >> SOF_ABI_MAJOR_SHIFT) & SOF_ABI_MAJOR_MASK)
+#define SOF_ABI_VERSION_MINOR(version)	\
+	(((version) >> SOF_ABI_MINOR_SHIFT) & SOF_ABI_MINOR_MASK)
+#define SOF_ABI_VERSION_PATCH(version)	\
+	(((version) >> SOF_ABI_PATCH_SHIFT) & SOF_ABI_PATCH_MASK)
+
+#define SOF_ABI_VERSION_INCOMPATIBLE(sof_ver, client_ver)		\
+	(SOF_ABI_VERSION_MAJOR((sof_ver)) !=				\
+		SOF_ABI_VERSION_MAJOR((client_ver))			\
+	)
+
+#define SOF_ABI_VERSION SOF_ABI_VER(SOF_ABI_MAJOR, SOF_ABI_MINOR, SOF_ABI_PATCH)
+
+/* SOF ABI magic number "SOF\0". */
+#define SOF_ABI_MAGIC		0x00464F53
+
+#endif
+
include/uapi/sound/sof/eq.h new file +164 -0

164 lines not on this page · raw message

include/uapi/sound/sof/fw.h new file +67 -0

67 lines not on this page · raw message

include/uapi/sound/sof/header.h new file +27 -0
new file mode 100644
index 000000000000..7868990b0d6f
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) */
+/*
+ * This file is provided under a dual BSD/GPLv2 license.  When using or
+ * redistributing this file, you may do so under either license.
+ *
+ * Copyright(c) 2018 Intel Corporation. All rights reserved.
+ */
+
+#ifndef __INCLUDE_UAPI_SOUND_SOF_USER_HEADER_H__
+#define __INCLUDE_UAPI_SOUND_SOF_USER_HEADER_H__
+
+/*
+ * Header for all non IPC ABI data.
+ *
+ * Identifies data type, size and ABI.
+ * Used by any bespoke component data structures or binary blobs.
+ */
+struct sof_abi_hdr {
+	uint32_t magic;		/**< 'S', 'O', 'F', '\0' */
+	uint32_t type;		/**< component specific type */
+	uint32_t size;		/**< size in bytes of data excl. this struct */
+	uint32_t abi;		/**< SOF ABI version */
+	uint32_t reserved[4];	/**< reserved for future use */
+	uint32_t data[0];	/**< Component data - opaque to core */
+}  __packed;
+
+#endif
include/uapi/sound/sof/manifest.h new file +188 -0

188 lines not on this page · raw message

include/uapi/sound/sof/tokens.h new file +95 -0

95 lines not on this page · raw message

include/uapi/sound/sof/tone.h new file +21 -0
new file mode 100644
index 000000000000..d7c6e5d8317e
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) */
+/*
+* This file is provided under a dual BSD/GPLv2 license.  When using or
+* redistributing this file, you may do so under either license.
+*
+* Copyright(c) 2018 Intel Corporation. All rights reserved.
+*/
+
+#ifndef __INCLUDE_UAPI_SOUND_SOF_USER_TONE_H__
+#define __INCLUDE_UAPI_SOUND_SOF_USER_TONE_H__
+
+#define SOF_TONE_IDX_FREQUENCY		0
+#define SOF_TONE_IDX_AMPLITUDE		1
+#define SOF_TONE_IDX_FREQ_MULT		2
+#define SOF_TONE_IDX_AMPL_MULT		3
+#define SOF_TONE_IDX_LENGTH		4
+#define SOF_TONE_IDX_PERIOD		5
+#define SOF_TONE_IDX_REPEATS		6
+#define SOF_TONE_IDX_LIN_RAMP_STEP	7
+
+#endif
include/uapi/sound/sof/trace.h new file +93 -0

93 lines not on this page · raw message

Signature · 6 lines
2.17.1

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel
#80 lore raw

Re: [PATCH v3 10/14] ASoC: SOF: Add userspace ABI support

<snip>
I have one question here, it is more related to the design of address space you are using.

Quoted 15 lines
include/uapi/sound/sof/fw.h new file +11 -0
 
+/*
+ * Firmware module is made up of 1 . N blocks of different types. The
+ * Block header is used to determine where and how block is to be copied in the
+ * DSP/host memory space.
+ */
 
+enum snd_sof_fw_blk_type {
+       SOF_BLK_IMAGE   = 0,    /* whole image - parsed by ROMs */
+       SOF_BLK_TEXT    = 1,
+       SOF_BLK_DATA    = 2,
+       SOF_BLK_CACHE   = 3,
+       SOF_BLK_REGS    = 4,

include/uapi/sound/sof/fw.h line 40

Quoted 10 lines
include/uapi/sound/sof/fw.h new file +10 -0
+       SOF_BLK_SIG     = 5,
+       SOF_BLK_ROM     = 6,
+       /* add new block types here */
+};
+
+struct snd_sof_blk_hdr {
+       enum snd_sof_fw_blk_type type;
+       uint32_t size;          /* bytes minus this header */
+       uint32_t offset;        /* offset from base */
+} __packed;

This assumes that all sections of the firmware are placed in the same "type" of memory area, right?

We can go with that for the moment.

On our side, because OCRAM is pretty small we found it useful to place some parts of the binary in SDRAM.

So, I'm wondering if you considered that? I think with the current design is fairly easy to support that. Just add inside snd_sof_blk_hdr also the "base", make the loader.c correctly parse that. Also, make rimage aware of this.

thanks,
Daniel.

#81 lore raw

Re: [Sound-open-firmware] [PATCH v3 10/14] ASoC: SOF: Add userspace ABI support

Quoted 30 lines · Daniel Baluta

<snip>
I have one question here, it is more related to the design of address space you are using.

Quoted 13 lines
include/uapi/sound/sof/fw.h new file +11 -0
+/*
+ * Firmware module is made up of 1 . N blocks of different types. The
+ * Block header is used to determine where and how block is to be copied in the
+ * DSP/host memory space.
+ */
+enum snd_sof_fw_blk_type {
+       SOF_BLK_IMAGE   = 0,    /* whole image - parsed by ROMs */
+       SOF_BLK_TEXT    = 1,
+       SOF_BLK_DATA    = 2,
+       SOF_BLK_CACHE   = 3,
+       SOF_BLK_REGS    = 4,

include/uapi/sound/sof/fw.h line 40

Quoted 10 lines
include/uapi/sound/sof/fw.h new file +10 -0
+       SOF_BLK_SIG     = 5,
+       SOF_BLK_ROM     = 6,
+       /* add new block types here */
+};
+
+struct snd_sof_blk_hdr {
+       enum snd_sof_fw_blk_type type;
+       uint32_t size;          /* bytes minus this header */
+       uint32_t offset;        /* offset from base */
+} __packed;

This assumes that all sections of the firmware are placed in the same "type" of memory area, right?

Quoted 8 lines

We can go with that for the moment.

On our side, because OCRAM is pretty small we found it useful to place some parts of the binary in SDRAM.

So, I'm wondering if you considered that? I think with the current design is fairly easy to support that. Just add inside snd_sof_blk_hdr also the "base", make the loader.c correctly parse that. Also, make rimage aware of this.

It's a generic requirement indeed. Even on Baytrail we could run from internal SRAM or external SDRAM, and a combination of the two. There are also firmware modules that need to be placed in specific memories with lower power, etc.

#82 lore raw

PATCH v3 11/14 ASoC: SOF: Add PM support

In reply to Pierre-Louis Bossart

From: Liam Girdwood <[email protected]>

Add support for saving and restoring DSP context in D3 to host DDR.

The suspend callback includes: suspend all pcm's stream that are running, send CTX_SAVE ipc, drop all ipc's, release trace dma and then power off the DSP.

And the resume callback performs the following steps: load FW, run FW, re-initialize trace, restore pipeline, restore the kcontrol values and finally send the ctx restore ipc to the dsp.

The streams that are suspended are resumed by the ALSA resume trigger. If the streams are paused during system suspend, they are marked explicitly so they can be restored during PAUSE_RELEASE.

---
 sound/soc/sof/pm.c | 403 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 403 insertions(+)
 create mode 100644 sound/soc/sof/pm.c
sound/soc/sof/pm.c new file +403 -0
new file mode 100644
index 000000000000..4e056bb0d7a4
@@ -0,0 +1,403 @@
+// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
+//
+// This file is provided under a dual BSD/GPLv2 license.  When using or
+// redistributing this file, you may do so under either license.
+//
+// Copyright(c) 2018 Intel Corporation. All rights reserved.
+//
+// Author: Liam Girdwood <[email protected]>
+//
+
+#include "ops.h"
+#include "sof-priv.h"
+
+#define RUNTIME_PM 1
+
+static int sof_restore_kcontrols(struct snd_sof_dev *sdev)
+{
+	struct snd_sof_control *scontrol = NULL;
+	int ipc_cmd, ctrl_type;
+	int ret = 0;
+
+	/* restore kcontrol values */
+	list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
+		/* reset readback offset for scontrol after resuming */
+		scontrol->readback_offset = 0;
+
+		/* notify DSP of kcontrol values */
+		switch (scontrol->cmd) {
+		case SOF_CTRL_CMD_VOLUME:
+			ipc_cmd = SOF_IPC_COMP_SET_VALUE;
+			ctrl_type = SOF_CTRL_TYPE_VALUE_CHAN_SET;
+			ret = snd_sof_ipc_set_comp_data(sdev->ipc, scontrol,
+							ipc_cmd, ctrl_type,
+							scontrol->cmd);
+			break;
+		case SOF_CTRL_CMD_BINARY:
+			ipc_cmd = SOF_IPC_COMP_SET_DATA;
+			ctrl_type = SOF_CTRL_TYPE_DATA_SET;
+			ret = snd_sof_ipc_set_comp_data(sdev->ipc, scontrol,
+							ipc_cmd, ctrl_type,
+							scontrol->cmd);
+			break;
+
+		default:
+			break;
+		}
+		if (ret < 0) {
+			dev_err(sdev->dev,
+				"error: failed kcontrol value set for widget: %d\n",
+				scontrol->comp_id);
+
+			return ret;
+		}
+	}
+
+	return 0;
+}
+
+static int sof_restore_pipelines(struct snd_sof_dev *sdev)
+{
+	struct snd_sof_widget *swidget = NULL;
+	struct snd_sof_route *sroute = NULL;
+	struct sof_ipc_pipe_new *pipeline;
+	struct snd_sof_dai *dai;
+	struct sof_ipc_comp_dai *comp_dai;
+	struct sof_ipc_cmd_hdr *hdr;
+	int ret = 0;
+
+	/* restore pipeline components */
+	list_for_each_entry_reverse(swidget, &sdev->widget_list, list) {
+		struct sof_ipc_comp_reply r;
+
+		/* skip if there is no private data */
+		if (!swidget->private)
+			continue;
+
+		switch (swidget->id) {
+		case snd_soc_dapm_dai_in:
+			/* fallthrough */
+		case snd_soc_dapm_dai_out:
+			dai = (struct snd_sof_dai *)swidget->private;
+			comp_dai = &dai->comp_dai;
+			ret = sof_ipc_tx_message(sdev->ipc,
+						 comp_dai->comp.hdr.cmd,
+						 comp_dai, sizeof(*comp_dai),
+						 &r, sizeof(r));
+			break;
+		case snd_soc_dapm_scheduler:
+
+			/*
+			 * During suspend, all DSP cores are powered off.
+			 * Therefore upon resume, create the pipeline comp
+			 * and power up the core that the pipeline is
+			 * scheduled on.
+			 */
+			pipeline = (struct sof_ipc_pipe_new *)swidget->private;
+			ret = sof_load_pipeline_ipc(sdev, pipeline, &r);
+			break;
+		default:
+			hdr = (struct sof_ipc_cmd_hdr *)swidget->private;
+			ret = sof_ipc_tx_message(sdev->ipc, hdr->cmd,
+						 swidget->private, hdr->size,
+						 &r, sizeof(r));
+			break;
+		}
+		if (ret < 0) {
+			dev_err(sdev->dev,
+				"error: failed to load widget type %d with ID: %d\n",
+				swidget->widget->id, swidget->comp_id);
+
+			return ret;
+		}
+	}
+
+	/* restore pipeline connections */
+	list_for_each_entry_reverse(sroute, &sdev->route_list, list) {
+		struct sof_ipc_pipe_comp_connect *connect;
+		struct sof_ipc_reply reply;
+
+		/* skip if there's no private data */
+		if (!sroute->private)
+			continue;
+
+		connect = sroute->private;
+
+		/* send ipc */
+		ret = sof_ipc_tx_message(sdev->ipc,
+					 connect->hdr.cmd,
+					 connect, sizeof(*connect),
+					 &reply, sizeof(reply));
+		if (ret < 0) {
+			dev_err(sdev->dev,
+				"error: failed to load route sink %s control %s source %s\n",
+				sroute->route.sink,
+				sroute->route.control ? sroute->route.control
+					: "none",
+				sroute->route.source);
+
+			return ret;
+		}
+	}
+
+	/* restore dai links */
+	list_for_each_entry_reverse(dai, &sdev->dai_list, list) {
+		struct sof_ipc_reply reply;
+		struct sof_ipc_dai_config *config = dai->dai_config;
+
+		ret = sof_ipc_tx_message(sdev->ipc,
+					 config->hdr.cmd, config,
+					 config->hdr.size,
+					 &reply, sizeof(reply));
+
+		if (ret < 0) {
+			dev_err(sdev->dev,
+				"error: failed to set dai config for %s\n",
+				dai->name);
+
+			return ret;
+		}
+	}
+
+	/* complete pipeline */
+	list_for_each_entry(swidget, &sdev->widget_list, list) {
+		switch (swidget->id) {
+		case snd_soc_dapm_scheduler:
+			swidget->complete =
+				snd_sof_complete_pipeline(sdev, swidget);
+			break;
+		default:
+			break;
+		}
+	}
+
+	/* restore pipeline kcontrols */
+	if (sdev->restore_kcontrols)
+		return sof_restore_kcontrols(sdev);
+
+	return 0;
+}
+
+static int sof_send_pm_ipc(struct snd_sof_dev *sdev, int cmd)
+{
+	struct sof_ipc_pm_ctx pm_ctx;
+	struct sof_ipc_reply reply;
+
+	memset(&pm_ctx, 0, sizeof(pm_ctx));
+
+	/* configure ctx save ipc message */
+	pm_ctx.hdr.size = sizeof(pm_ctx);
+	pm_ctx.hdr.cmd = SOF_IPC_GLB_PM_MSG | cmd;
+
+	/* send ctx save ipc to dsp */
+	return sof_ipc_tx_message(sdev->ipc, pm_ctx.hdr.cmd, &pm_ctx,
+				 sizeof(pm_ctx), &reply, sizeof(reply));
+}
+
+static void sof_suspend_streams(struct snd_sof_dev *sdev)
+{
+	struct snd_sof_pcm *spcm;
+	struct snd_pcm_substream *substream;
+	int dir;
+
+	/* suspend all running streams */
+	list_for_each_entry(spcm, &sdev->pcm_list, list) {
+
+		mutex_lock(&spcm->mutex);
+
+		/* suspend running playback stream */
+		dir = SNDRV_PCM_STREAM_PLAYBACK;
+		substream = spcm->stream[dir].substream;
+
+		if (substream && substream->runtime) {
+
+			snd_pcm_suspend(substream);
+
+			/*
+			 * set restore_stream so that hw_params can be
+			 * restored during resume
+			 */
+			spcm->restore_stream[dir] = 1;
+		}
+
+		/* suspend running capture stream */
+		dir = SNDRV_PCM_STREAM_CAPTURE;
+		substream = spcm->stream[dir].substream;
+
+		if (substream && substream->runtime) {
+
+			snd_pcm_suspend(substream);
+
+			/*
+			 * set restore_stream so that hw_params can be
+			 * restored during resume
+			 */
+			spcm->restore_stream[dir] = 1;
+		}
+
+		mutex_unlock(&spcm->mutex);
+	}
+}
+
+static int sof_resume(struct device *dev, int runtime_resume)
+{
+	struct sof_platform_priv *priv = dev_get_drvdata(dev);
+	struct snd_sof_dev *sdev = dev_get_drvdata(&priv->pdev_pcm->dev);
+	int ret = 0;
+
+	/* do nothing if dsp resume callbacks are not set */
+	if (!sdev->ops->resume || !sdev->ops->runtime_resume)
+		return 0;
+
+	/*
+	 * if the runtime_resume flag is set, call the runtime_resume routine
+	 * or else call the system resume routine
+	 */
+	if (runtime_resume)
+		ret = snd_sof_dsp_runtime_resume(sdev);
+	else
+		ret = snd_sof_dsp_resume(sdev);
+	if (ret < 0) {
+		dev_err(sdev->dev,
+			"error: failed to power up DSP after resume\n");
+		return ret;
+	}
+
+	/* load the firmware */
+	ret = snd_sof_load_firmware(sdev);
+	if (ret < 0) {
+		dev_err(sdev->dev,
+			"error: failed to load DSP firmware after resume %d\n",
+			ret);
+		return ret;
+	}
+
+	/* boot the firmware */
+	ret = snd_sof_run_firmware(sdev);
+	if (ret < 0) {
+		dev_err(sdev->dev,
+			"error: failed to boot DSP firmware after resume %d\n",
+			ret);
+		return ret;
+	}
+
+	/* resume DMA trace, only need send ipc */
+	ret = snd_sof_init_trace_ipc(sdev);
+	if (ret < 0) {
+		/* non fatal */
+		dev_warn(sdev->dev,
+			 "warning: failed to init trace after resume %d\n",
+			 ret);
+	}
+
+	/* restore pipelines */
+	ret = sof_restore_pipelines(sdev);
+	if (ret < 0) {
+		dev_err(sdev->dev,
+			"error: failed to restore pipeline after resume %d\n",
+			ret);
+		return ret;
+	}
+
+	/* notify DSP of system resume */
+	ret = sof_send_pm_ipc(sdev, SOF_IPC_PM_CTX_RESTORE);
+	if (ret < 0)
+		dev_err(sdev->dev,
+			"error: ctx_restore ipc error during resume %d\n",
+			ret);
+
+	return ret;
+}
+
+static int sof_suspend(struct device *dev, int runtime_suspend)
+{
+	struct sof_platform_priv *priv = dev_get_drvdata(dev);
+	struct snd_sof_dev *sdev = dev_get_drvdata(&priv->pdev_pcm->dev);
+	int ret = 0;
+
+	/* do nothing if dsp suspend callback is not set */
+	if (!sdev->ops->suspend)
+		return 0;
+
+	/* release trace */
+	snd_sof_release_trace(sdev);
+
+	/*
+	 * Suspend running pcm streams.
+	 * They will be restarted by ALSA resume trigger call.
+	 */
+	sof_suspend_streams(sdev);
+
+	/* notify DSP of upcoming power down */
+	ret = sof_send_pm_ipc(sdev, SOF_IPC_PM_CTX_SAVE);
+	if (ret < 0) {
+		dev_err(sdev->dev,
+			"error: ctx_save ipc error during suspend %d\n",
+			ret);
+		return ret;
+	}
+
+	/* drop all ipc */
+	sof_ipc_drop_all(sdev->ipc);
+
+	/* power down all DSP cores */
+	if (runtime_suspend)
+		ret = snd_sof_dsp_runtime_suspend(sdev, 0);
+	else
+		ret = snd_sof_dsp_suspend(sdev, 0);
+	if (ret < 0)
+		dev_err(sdev->dev,
+			"error: failed to power down DSP during suspend %d\n",
+			ret);
+
+	/* set flag for restoring kcontrols upon resuming */
+	sdev->restore_kcontrols = true;
+
+	return ret;
+}
+
+int snd_sof_runtime_suspend(struct device *dev)
+{
+	return sof_suspend(dev, RUNTIME_PM);
+}
+EXPORT_SYMBOL(snd_sof_runtime_suspend);
+
+int snd_sof_runtime_resume(struct device *dev)
+{
+	return sof_resume(dev, RUNTIME_PM);
+}
+EXPORT_SYMBOL(snd_sof_runtime_resume);
+
+int snd_sof_resume(struct device *dev)
+{
+	return sof_resume(dev, !RUNTIME_PM);
+}
+EXPORT_SYMBOL(snd_sof_resume);
+
+int snd_sof_suspend(struct device *dev)
+{
+	return 0;
+}
+EXPORT_SYMBOL(snd_sof_suspend);
+
+int snd_sof_suspend_late(struct device *dev)
+{
+	return sof_suspend(dev, !RUNTIME_PM);
+}
+EXPORT_SYMBOL(snd_sof_suspend_late);
+
+int snd_sof_prepare(struct device *dev)
+{
+	struct sof_platform_priv *priv = dev_get_drvdata(dev);
+	struct snd_sof_dev *sdev = dev_get_drvdata(&priv->pdev_pcm->dev);
+
+	/*
+	 * PCI devices are brought back to full power before system suspend.
+	 * Setting this flag will prevent restoring kcontrols
+	 * when resuming before system suspend
+	 */
+	sdev->restore_kcontrols = false;
+
+	return 0;
+}
+EXPORT_SYMBOL(snd_sof_prepare);
Signature · 1 line
2.17.1
#83 lore raw

Re: [PATCH v3 11/14] ASoC: SOF: Add PM support

Quoted 2 lines · Pierre-Louis Bossart

+#define RUNTIME_PM 1

What's this? This seems used in snd_soc_runtime_suspend() and _resume(). But it's a just normal boolean flag, no?

Quoted 30 lines
sound/soc/sof/pm.c new file +30 -0
+static void sof_suspend_streams(struct snd_sof_dev *sdev)
+{
+	struct snd_sof_pcm *spcm;
+	struct snd_pcm_substream *substream;
+	int dir;
+
+	/* suspend all running streams */
+	list_for_each_entry(spcm, &sdev->pcm_list, list) {
+
+		mutex_lock(&spcm->mutex);
+
+		/* suspend running playback stream */
+		dir = SNDRV_PCM_STREAM_PLAYBACK;
+		substream = spcm->stream[dir].substream;
+
+		if (substream && substream->runtime) {
+
+			snd_pcm_suspend(substream);
+
+			/*
+			 * set restore_stream so that hw_params can be
+			 * restored during resume
+			 */
+			spcm->restore_stream[dir] = 1;
+		}
+
+		/* suspend running capture stream */
+		dir = SNDRV_PCM_STREAM_CAPTURE;
+		substream = spcm->stream[dir].substream;
+

sound/soc/sof/pm.c line 236

Quoted 10 lines
sound/soc/sof/pm.c new file +10 -0
+		if (substream && substream->runtime) {
+
+			snd_pcm_suspend(substream);
+
+			/*
+			 * set restore_stream so that hw_params can be
+			 * restored during resume
+			 */
+			spcm->restore_stream[dir] = 1;
+		}

Both playback and capture do the same thing, so this can be a loop of dir.

thanks,

Takashi

#84 lore raw

Re: [PATCH v3 11/14] ASoC: SOF: Add PM support

Quoted 5 lines · Takashi Iwai
Quoted 1 line · Pierre-Louis Bossart

+#define RUNTIME_PM 1

What's this? This seems used in snd_soc_runtime_suspend() and _resume(). But it's a just normal boolean flag, no?

yes, it's useless. I saw it in one of my checks and forgot about it, will fix.

Quoted 35 lines
Quoted 35 lines
sound/soc/sof/pm.c new file +34 -0
+static void sof_suspend_streams(struct snd_sof_dev *sdev)
+{
+	struct snd_sof_pcm *spcm;
+	struct snd_pcm_substream *substream;
+	int dir;
+
+	/* suspend all running streams */
+	list_for_each_entry(spcm, &sdev->pcm_list, list) {
+
+		mutex_lock(&spcm->mutex);
+
+		/* suspend running playback stream */
+		dir = SNDRV_PCM_STREAM_PLAYBACK;
+		substream = spcm->stream[dir].substream;
+
+		if (substream && substream->runtime) {
+
+			snd_pcm_suspend(substream);
+
+			/*
+			 * set restore_stream so that hw_params can be
+			 * restored during resume
+			 */
+			spcm->restore_stream[dir] = 1;
+		}
+
+		/* suspend running capture stream */
+		dir = SNDRV_PCM_STREAM_CAPTURE;
+		substream = spcm->stream[dir].substream;
+
+		if (substream && substream->runtime) {
+
+			snd_pcm_suspend(substream);
+
Quoted 8 lines

sound/soc/sof/pm.c line 221

Quoted 6 lines
sound/soc/sof/pm.c new file +6 -0
+			/*
+			 * set restore_stream so that hw_params can be
+			 * restored during resume
+			 */
+			spcm->restore_stream[dir] = 1;
+		}

Both playback and capture do the same thing, so this can be a loop of dir.

yes, valid point, will fix.

Quoted 9 lines

thanks,

Takashi
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#85 lore raw

PATCH v3 12/14 ASoC: SOF: Add Nocodec machine driver support

In reply to Pierre-Louis Bossart

From: Liam Girdwood <[email protected]>

Add a simple "fallback" machine driver that can be used to enable SOF on boards with no codec device. This machine driver can also be forced for debug/development.

---
 include/sound/sof.h     | 10 +++++
 sound/soc/sof/nocodec.c | 81 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 91 insertions(+)
 create mode 100644 sound/soc/sof/nocodec.c
include/sound/sof.h +10 -0
index 371a14cb4a56..4ed6e2379141 100644
@@ -12,6 +12,7 @@
 #define __INCLUDE_SOUND_SOF_H
 
 #include <linux/pci.h>
+#include <sound/soc.h>
 #include <sound/soc-acpi.h>
 
 struct snd_sof_dsp_ops;
@@ -78,4 +79,13 @@ struct sof_dev_desc {
 	const char *nocodec_tplg_filename;
 };
 
+int sof_nocodec_setup(struct device *dev,
+		      struct snd_sof_pdata *sof_pdata,
+		      struct snd_soc_acpi_mach *mach,
+		      const struct sof_dev_desc *desc,
+		      struct snd_sof_dsp_ops *ops);
+
+int sof_bes_setup(struct device *dev, struct snd_sof_dsp_ops *ops,
+		  struct snd_soc_dai_link *links, int link_num,
+		  struct snd_soc_card *card);
 #endif
sound/soc/sof/nocodec.c new file +81 -0

81 lines not on this page · raw message

Signature · 1 line
2.17.1
#86 lore raw

PATCH v3 13/14 ASoC: SOF: Add xtensa support

In reply to Pierre-Louis Bossart

Add common directory for xtensa architecture

---
 include/sound/sof/xtensa.h    |  44 ++++++++++
 sound/soc/sof/xtensa/Kconfig  |   3 +
 sound/soc/sof/xtensa/Makefile |   5 ++
 sound/soc/sof/xtensa/core.c   | 152 ++++++++++++++++++++++++++++++++++
 4 files changed, 204 insertions(+)
 create mode 100644 include/sound/sof/xtensa.h
 create mode 100644 sound/soc/sof/xtensa/Kconfig
 create mode 100644 sound/soc/sof/xtensa/Makefile
 create mode 100644 sound/soc/sof/xtensa/core.c
include/sound/sof/xtensa.h new file +44 -0
new file mode 100644
index 000000000000..a7189984000d
@@ -0,0 +1,44 @@
+/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) */
+/*
+ * This file is provided under a dual BSD/GPLv2 license.  When using or
+ * redistributing this file, you may do so under either license.
+ *
+ * Copyright(c) 2018 Intel Corporation. All rights reserved.
+ */
+
+#ifndef __INCLUDE_SOUND_SOF_XTENSA_H__
+#define __INCLUDE_SOUND_SOF_XTENSA_H__
+
+#include <sound/sof/header.h>
+
+/*
+ * Architecture specific debug
+ */
+
+/* Xtensa Firmware Oops data */
+struct sof_ipc_dsp_oops_xtensa {
+	struct sof_ipc_hdr hdr;
+	uint32_t exccause;
+	uint32_t excvaddr;
+	uint32_t ps;
+	uint32_t epc1;
+	uint32_t epc2;
+	uint32_t epc3;
+	uint32_t epc4;
+	uint32_t epc5;
+	uint32_t epc6;
+	uint32_t epc7;
+	uint32_t eps2;
+	uint32_t eps3;
+	uint32_t eps4;
+	uint32_t eps5;
+	uint32_t eps6;
+	uint32_t eps7;
+	uint32_t depc;
+	uint32_t intenable;
+	uint32_t interrupt;
+	uint32_t sar;
+	uint32_t stack;
+}  __packed;
+
+#endif
sound/soc/sof/xtensa/Kconfig new file +3 -0
new file mode 100644
index 000000000000..f66f17bb0335
@@ -0,0 +1,3 @@
+config SND_SOC_SOF_XTENSA
+	tristate
+
sound/soc/sof/xtensa/Makefile new file +5 -0
new file mode 100644
index 000000000000..cc89c7472a38
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
+
+snd-sof-xtensa-dsp-objs := core.o
+
+obj-$(CONFIG_SND_SOC_SOF_XTENSA) += snd-sof-xtensa-dsp.o
sound/soc/sof/xtensa/core.c new file +152 -0

152 lines not on this page · raw message

Signature · 6 lines
2.17.1

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel
#87 lore raw

Re: [PATCH v3 13/14] ASoC: SOF: Add xtensa support

Quoted 19 lines · Pierre-Louis Bossart

Add common directory for xtensa architecture

sound/soc/sof/xtensa/core.c new file +17 -0
+	for (i = 0; i <= stack_words - 4; i += 4) {
+		dev_err(sdev->dev, "0x%8.8x: 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x\n",
+			stack_ptr + i, stack[i], stack[i + 1], stack[i + 2],
+			stack[i + 3]);
+	}
+
+	/* deal with any remaining words */
+	switch (stack_words - i) {
+	case 0:
+		break;
+	case 1:
+		dev_err(sdev->dev, "0x%8.8x: 0x%8.8x\n",
+			stack_ptr + stack_words - 1, stack[stack_words - 1]);
+		break;
+	case 2:
+		dev_err(sdev->dev, "0x%8.8x: 0x%8.8x 0x%8.8x\n",
+			stack_ptr + stack_words - 2, stack[stack_words - 2],

sound/soc/sof/xtensa/core.c line 142

Quoted 10 lines
sound/soc/sof/xtensa/core.c new file +10 -0
+			stack[stack_words - 1]);
+		break;
+	case 3:
+		dev_err(sdev->dev, "0x%8.8x: 0x%8.8x 0x%8.8x 0x%8.8x\n",
+			stack_ptr + stack_words - 3, stack[stack_words - 3],
+			stack[stack_words - 2], stack[stack_words - 1]);
+		break;
+	default:
+		break;
+	}

hex_dump_to_buffer().

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#88 lore raw

Re: [PATCH v3 13/14] ASoC: SOF: Add xtensa support

Quoted 22 lines · Andy Shevchenko
Quoted 21 lines · Pierre-Louis Bossart

Add common directory for xtensa architecture

sound/soc/sof/xtensa/core.c new file +20 -0
+	for (i = 0; i <= stack_words - 4; i += 4) {
+		dev_err(sdev->dev, "0x%8.8x: 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x\n",
+			stack_ptr + i, stack[i], stack[i + 1], stack[i + 2],
+			stack[i + 3]);
+	}
+
+	/* deal with any remaining words */
+	switch (stack_words - i) {
+	case 0:
+		break;
+	case 1:
+		dev_err(sdev->dev, "0x%8.8x: 0x%8.8x\n",
+			stack_ptr + stack_words - 1, stack[stack_words - 1]);
+		break;
+	case 2:
+		dev_err(sdev->dev, "0x%8.8x: 0x%8.8x 0x%8.8x\n",
+			stack_ptr + stack_words - 2, stack[stack_words - 2],
+			stack[stack_words - 1]);
+		break;
+	case 3:
Quoted 8 lines

sound/soc/sof/xtensa/core.c line 142

Quoted 7 lines
sound/soc/sof/xtensa/core.c new file +7 -0
+		dev_err(sdev->dev, "0x%8.8x: 0x%8.8x 0x%8.8x 0x%8.8x\n",
+			stack_ptr + stack_words - 3, stack[stack_words - 3],
+			stack[stack_words - 2], stack[stack_words - 1]);
+		break;
+	default:
+		break;
+	}

hex_dump_to_buffer().

I vaguely recall there was a reason not to do it, we'll relook at this.

A message that is not in the archive (sent off-list or not yet received): [email protected]

#90 lore raw

Re: [Sound-open-firmware] [PATCH v3 13/14] ASoC: SOF: Add xtensa support

Quoted 41 lines · rander.wang
Quoted 32 lines · Pierre-Louis Bossart
Quoted 30 lines · Andy Shevchenko
Quoted 29 lines · Pierre-Louis Bossart

Add common directory for xtensa architecture
+    for (i = 0; i <= stack_words - 4; i += 4) {
+        dev_err(sdev->dev, "0x%8.8x: 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x\n",
+            stack_ptr + i, stack[i], stack[i + 1], stack[i + 2], +            stack[i + 3]);
+    }
+
+    /* deal with any remaining words */
+    switch (stack_words - i) {
+    case 0:
+        break;
+    case 1:
+        dev_err(sdev->dev, "0x%8.8x: 0x%8.8x\n",
+            stack_ptr + stack_words - 1, stack[stack_words - 1]); +        break;
+    case 2:
+        dev_err(sdev->dev, "0x%8.8x: 0x%8.8x 0x%8.8x\n", +            stack_ptr + stack_words - 2, stack[stack_words - 2], +            stack[stack_words - 1]);
+        break;
+    case 3:
+        dev_err(sdev->dev, "0x%8.8x: 0x%8.8x 0x%8.8x 0x%8.8x\n", +            stack_ptr + stack_words - 3, stack[stack_words - 3], +            stack[stack_words - 2], stack[stack_words - 1]); +        break;
+    default:
+        break;
+    }

Quoted 3 lines
Quoted 1 line

hex_dump_to_buffer().

I vaguely recall there was a reason not to do it, we'll relook at this.

we don't want to convert a blob of data to "hex ASCII" in memory (hex_dump_to_buffer).

Here, we just want to dump the content of call stack, like:

Quoted 8 lines

[    3.250628] sof-audio sof-audio: 0x0049fbb0: 0x8000f2d0 0x0049fc00 0x6f6c6c61 0x00632e63 [    3.250670] sof-audio sof-audio: 0x0049fbb4: 0x00ffffff 0x0010b5d4 0x00000000 0x2000004c [    3.250710] sof-audio sof-audio: 0x0049fbb8: 0x0040d000 0x00000000 0x0000024c 0x2000930c [    3.250747] sof-audio sof-audio: 0x0049fbbc: 0x8000454c 0x0049fc30 0x00401e20 0x00000001 [    3.250787] sof-audio sof-audio: 0x0049fbc0: 0x8000454c 0x0049fc30 0x00401e20 0x00000001 [    3.250827] sof-audio sof-audio: 0x0049fbc4: 0x00000000 0x00000001 0x00000010 0x0049fcb8 [    3.250865] sof-audio sof-audio: 0x0049fbc8: 0x0040a000 0x00401e98 0x00000001 0x00000002 [    3.250889] sof-audio sof-audio: 0x0049fbcc: 0x80005ac5 0x0049fc60 0x00000000 0x00000000

Add more details: we use another tool to analysis the hex numbers with a ELF file to get the correct information, so we do it by ourself.

Thanks!

Rander

Quoted 4 lines
Quoted 4 lines

_______________________________________________
Sound-open-firmware mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/sound-open-firmware

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#91 lore raw

Re: [Sound-open-firmware] [PATCH v3 13/14] ASoC: SOF: Add xtensa support

Quoted 62 lines · rander.wang
Quoted 42 lines · rander.wang
Quoted 32 lines · Pierre-Louis Bossart
Quoted 30 lines · Andy Shevchenko
Quoted 29 lines · Pierre-Louis Bossart

Add common directory for xtensa architecture
+    for (i = 0; i <= stack_words - 4; i += 4) {
+        dev_err(sdev->dev, "0x%8.8x: 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x\n",
+            stack_ptr + i, stack[i], stack[i + 1], stack[i + 2], +            stack[i + 3]);
+    }
+
+    /* deal with any remaining words */
+    switch (stack_words - i) {
+    case 0:
+        break;
+    case 1:
+        dev_err(sdev->dev, "0x%8.8x: 0x%8.8x\n",
+            stack_ptr + stack_words - 1, stack[stack_words - 1]); +        break;
+    case 2:
+        dev_err(sdev->dev, "0x%8.8x: 0x%8.8x 0x%8.8x\n", +            stack_ptr + stack_words - 2, stack[stack_words - 2], +            stack[stack_words - 1]);
+        break;
+    case 3:
+        dev_err(sdev->dev, "0x%8.8x: 0x%8.8x 0x%8.8x 0x%8.8x\n", +            stack_ptr + stack_words - 3, stack[stack_words - 3], +            stack[stack_words - 2], stack[stack_words - 1]); +        break;
+    default:
+        break;
+    }

Quoted 3 lines
Quoted 1 line

hex_dump_to_buffer().

I vaguely recall there was a reason not to do it, we'll relook at this.

we don't want to convert a blob of data to "hex ASCII" in memory (hex_dump_to_buffer).

Here, we just want to dump the content of call stack, like:

Quoted 16 lines

[    3.250628] sof-audio sof-audio: 0x0049fbb0: 0x8000f2d0 0x0049fc00 0x6f6c6c61 0x00632e63
[    3.250670] sof-audio sof-audio: 0x0049fbb4: 0x00ffffff 0x0010b5d4 0x00000000 0x2000004c
[    3.250710] sof-audio sof-audio: 0x0049fbb8: 0x0040d000 0x00000000 0x0000024c 0x2000930c
[    3.250747] sof-audio sof-audio: 0x0049fbbc: 0x8000454c 0x0049fc30 0x00401e20 0x00000001
[    3.250787] sof-audio sof-audio: 0x0049fbc0: 0x8000454c 0x0049fc30 0x00401e20 0x00000001
[    3.250827] sof-audio sof-audio: 0x0049fbc4: 0x00000000 0x00000001 0x00000010 0x0049fcb8
[    3.250865] sof-audio sof-audio: 0x0049fbc8: 0x0040a000 0x00401e98 0x00000001 0x00000002
[    3.250889] sof-audio sof-audio: 0x0049fbcc: 0x80005ac5 0x0049fc60 0x00000000 0x00000000

Quoted 2 lines

Add more details: we use another tool to analysis the hex numbers with a ELF file to get the correct information, so we do it by ourself.

If the format is intentional and shouldn't be changed to the generic one, this has to be mentioned in the code as some proper comment.

The use of hex_dump_to_buffer() or whatever the standard macro is a "right thing" in general. From what I can see, the home-baked code does nothing but writing in 32bit words, so without the explanation, no one can see the reason to stick with it.

thanks,

Takashi
_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

maintainer
#92 lore raw

Re: [Sound-open-firmware] [PATCH v3 13/14] ASoC: SOF: Add xtensa support

Quoted 8 lines · Takashi Iwai
Quoted 5 lines · rander.wang
Quoted 2 lines

[    3.250889] sof-audio sof-audio: 0x0049fbcc: 0x80005ac5 0x0049fc60 0x00000000 0x00000000

Add more details: we use another tool to analysis the hex numbers with a ELF file to get the correct information, so we do it by ourself.

Quoted 7 lines

If the format is intentional and shouldn't be changed to the generic one, this has to be mentioned in the code as some proper comment.

The use of hex_dump_to_buffer() or whatever the standard macro is a "right thing" in general. From what I can see, the home-baked code does nothing but writing in 32bit words, so without the explanation, no one can see the reason to stick with it.

Or alternatively can the tool be changed to parse the more standard format if it's not too hard to get people to update?

#93 lore raw

PATCH v3 14/14 ASoC: SOF: Add utils

In reply to Pierre-Louis Bossart

Helpers to set-up back-ends, create platform devices and common IO/block read/write operations

---
 sound/soc/sof/utils.c | 173 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 173 insertions(+)
 create mode 100644 sound/soc/sof/utils.c
sound/soc/sof/utils.c new file +173 -0
new file mode 100644
index 000000000000..bf66e14bc258
@@ -0,0 +1,173 @@
+// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
+//
+// This file is provided under a dual BSD/GPLv2 license.  When using or
+// redistributing this file, you may do so under either license.
+//
+// Copyright(c) 2018 Intel Corporation. All rights reserved.
+//
+// Author: Keyon Jie <[email protected]>
+//
+
+#include <linux/io.h>
+#include <linux/platform_device.h>
+#include <sound/soc.h>
+#include <sound/sof.h>
+#include "sof-priv.h"
+
+int sof_bes_setup(struct device *dev, struct snd_sof_dsp_ops *ops,
+		  struct snd_soc_dai_link *links, int link_num,
+		  struct snd_soc_card *card)
+{
+	char name[32];
+	int i;
+
+	if (!ops || !links || !card)
+		return -EINVAL;
+
+	/* set up BE dai_links */
+	for (i = 0; i < link_num; i++) {
+		snprintf(name, 32, "NoCodec-%d", i);
+		links[i].name = devm_kstrdup(dev, name, GFP_KERNEL);
+		if (!links[i].name)
+			return -ENOMEM;
+
+		links[i].id = i;
+		links[i].no_pcm = 1;
+		links[i].cpu_dai_name = ops->drv[i].name;
+		links[i].platform_name = "sof-audio";
+		links[i].codec_dai_name = "snd-soc-dummy-dai";
+		links[i].codec_name = "snd-soc-dummy";
+		links[i].dpcm_playback = 1;
+		links[i].dpcm_capture = 1;
+	}
+
+	card->dai_link = links;
+	card->num_links = link_num;
+
+	return 0;
+}
+EXPORT_SYMBOL(sof_bes_setup);
+
+/* register sof platform device */
+int sof_create_platform_device(struct sof_platform_priv *priv)
+{
+	struct snd_sof_pdata *sof_pdata = priv->sof_pdata;
+	struct device *dev = sof_pdata->dev;
+
+	priv->pdev_pcm =
+		platform_device_register_data(dev, "sof-audio", -1,
+					      sof_pdata, sizeof(*sof_pdata));
+	if (IS_ERR(priv->pdev_pcm)) {
+		dev_err(dev, "error: cannot register device sof-audio. Error %d\n",
+			(int)PTR_ERR(priv->pdev_pcm));
+		return PTR_ERR(priv->pdev_pcm);
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(sof_create_platform_device);
+
+/*
+ * Register IO
+ */
+
+void sof_io_write(struct snd_sof_dev *sdev, void __iomem *addr, u32 value)
+{
+	writel(value, addr);
+}
+EXPORT_SYMBOL(sof_io_write);
+
+u32 sof_io_read(struct snd_sof_dev *sdev, void __iomem *addr)
+{
+	return readl(addr);
+}
+EXPORT_SYMBOL(sof_io_read);
+
+void sof_io_write64(struct snd_sof_dev *sdev, void __iomem *addr, u64 value)
+{
+#ifdef CONFIG_64BIT
+	writeq(value, addr);
+#else
+	memcpy_toio(addr, &value, sizeof(value));
+#endif
+}
+EXPORT_SYMBOL(sof_io_write64);
+
+u64 sof_io_read64(struct snd_sof_dev *sdev, void __iomem *addr)
+{
+#ifdef CONFIG_64BIT
+	return readq(addr);
+#else
+	u64 val;
+
+	memcpy_fromio(&val, addr, sizeof(val));
+	return val;
+#endif
+}
+EXPORT_SYMBOL(sof_io_read64);
+
+/*
+ * IPC Mailbox IO
+ */
+
+void sof_mailbox_write(struct snd_sof_dev *sdev, u32 offset,
+		       void *message, size_t bytes)
+{
+	void __iomem *dest = sdev->bar[sdev->mailbox_bar] + offset;
+
+	memcpy_toio(dest, message, bytes);
+}
+EXPORT_SYMBOL(sof_mailbox_write);
+
+void sof_mailbox_read(struct snd_sof_dev *sdev, u32 offset,
+		      void *message, size_t bytes)
+{
+	void __iomem *src = sdev->bar[sdev->mailbox_bar] + offset;
+
+	memcpy_fromio(message, src, bytes);
+}
+EXPORT_SYMBOL(sof_mailbox_read);
+
+/*
+ * Memory copy.
+ */
+
+void sof_block_write(struct snd_sof_dev *sdev, u32 offset, void *src,
+		     size_t size)
+{
+	void __iomem *dest = sdev->bar[sdev->mmio_bar] + offset;
+	const u8 *src_byte = src;
+	u32 affected_mask;
+	u32 tmp = 0;
+	int m, n;
+
+	m = size / 4;
+	n = size % 4;
+
+	/* __iowrite32_copy use 32bit size values so divide by 4 */
+	__iowrite32_copy(dest, src, m);
+
+	if (n) {
+		affected_mask = (1 << (8 * n)) - 1;
+
+		/* first read the 32bit data of dest, then change affected
+		 * bytes, and write back to dest. For unaffected bytes, it
+		 * should not be changed
+		 */
+		__ioread32_copy(&tmp, dest + m * 4, 1);
+		tmp &= ~affected_mask;
+
+		tmp |= *(u32 *)(src_byte + m * 4) & affected_mask;
+		__iowrite32_copy(dest + m * 4, &tmp, 1);
+	}
+}
+EXPORT_SYMBOL(sof_block_write);
+
+void sof_block_read(struct snd_sof_dev *sdev, u32 offset, void *dest,
+		    size_t size)
+{
+	void __iomem *src = sdev->bar[sdev->mmio_bar] + offset;
+
+	memcpy_fromio(dest, src, size);
+}
+EXPORT_SYMBOL(sof_block_read);
Signature · 1 line
2.17.1
#94 lore raw

Re: [PATCH v3 14/14] ASoC: SOF: Add utils

Quoted 7 lines · Pierre-Louis Bossart

Helpers to set-up back-ends, create platform devices and common IO/block read/write operations

sound/soc/sof/utils.c new file +4 -0
+int sof_bes_setup(struct device *dev, struct snd_sof_dsp_ops *ops,
+		  struct snd_soc_dai_link *links, int link_num,
+		  struct snd_soc_card *card)
+{

sound/soc/sof/utils.c line 29

Quoted 10 lines
sound/soc/sof/utils.c new file +9 -0
+	char name[32];
+	int i;
+
+	if (!ops || !links || !card)
+		return -EINVAL;
+
+	/* set up BE dai_links */
+	for (i = 0; i < link_num; i++) {
 
+		snprintf(name, 32, "NoCodec-%d", i);

sizeof(name) ?

sound/soc/sof/utils.c line 32

Quoted 3 lines
sound/soc/sof/utils.c new file +3 -0
+		links[i].name = devm_kstrdup(dev, name, GFP_KERNEL);
+		if (!links[i].name)
+			return -ENOMEM;

...or better devm_kasprintf().

Quoted 20 lines
sound/soc/sof/utils.c new file +20 -0
+
+		links[i].id = i;
+		links[i].no_pcm = 1;
+		links[i].cpu_dai_name = ops->drv[i].name;
+		links[i].platform_name = "sof-audio";
+		links[i].codec_dai_name = "snd-soc-dummy-dai";
+		links[i].codec_name = "snd-soc-dummy";
+		links[i].dpcm_playback = 1;
+		links[i].dpcm_capture = 1;
+	}
+
+	card->dai_link = links;
+	card->num_links = link_num;
+
+	return 0;
+}
+EXPORT_SYMBOL(sof_bes_setup);
+
+/* register sof platform device */
+int sof_create_platform_device(struct sof_platform_priv *priv)

sound/soc/sof/utils.c line 62

Quoted 10 lines
sound/soc/sof/utils.c new file +10 -0
+{
+	struct snd_sof_pdata *sof_pdata = priv->sof_pdata;
+	struct device *dev = sof_pdata->dev;
+
+	priv->pdev_pcm =
+		platform_device_register_data(dev, "sof-audio", -1,
+					      sof_pdata, sizeof(*sof_pdata));
+	if (IS_ERR(priv->pdev_pcm)) {
+		dev_err(dev, "error: cannot register device sof-audio. Error %d\n",
+			(int)PTR_ERR(priv->pdev_pcm));

Instead of casting perhaps use %ld?

Quoted 4 lines
sound/soc/sof/utils.c new file +4 -0
+		return PTR_ERR(priv->pdev_pcm);
+	}
+
+	return 0;

sound/soc/sof/utils.c line 92

Quoted 10 lines
sound/soc/sof/utils.c new file +9 -0
+}
+EXPORT_SYMBOL(sof_create_platform_device);
 
+void sof_io_write64(struct snd_sof_dev *sdev, void __iomem *addr, u64 value)
+{
+#ifdef CONFIG_64BIT
+	writeq(value, addr);
+#else
+	memcpy_toio(addr, &value, sizeof(value));
+#endif

This ifdef sounds strange.
Just include io64-nonatomic-lo-hi.h and use readq() w/o limitations.

sound/soc/sof/utils.c line 105

Quoted 13 lines
sound/soc/sof/utils.c new file +13 -0
+}
+EXPORT_SYMBOL(sof_io_write64);
+
+u64 sof_io_read64(struct snd_sof_dev *sdev, void __iomem *addr)
+{
+#ifdef CONFIG_64BIT
+	return readq(addr);
+#else
+	u64 val;
+
+	memcpy_fromio(&val, addr, sizeof(val));
+	return val;
+#endif

Ditto.

Quoted 5 lines
sound/soc/sof/utils.c new file +4 -0
+}
+EXPORT_SYMBOL(sof_io_read64);
 
+		affected_mask = (1 << (8 * n)) - 1;
+

sound/soc/sof/utils.c line 161

Quoted 10 lines
sound/soc/sof/utils.c new file +9 -0
+		/* first read the 32bit data of dest, then change affected
+		 * bytes, and write back to dest. For unaffected bytes, it
+		 * should not be changed
+		 */
 
+		__ioread32_copy(&tmp, dest + m * 4, 1);
+		tmp &= ~affected_mask;
+
+		tmp |= *(u32 *)(src_byte + m * 4) & affected_mask;
+		__iowrite32_copy(dest + m * 4, &tmp, 1);

Ain't these equivalents to simple ioread32() / iowrite32() ?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#95 lore raw

Re: [PATCH v3 14/14] ASoC: SOF: Add utils

Quoted 17 lines · Andy Shevchenko
Quoted 5 lines · Pierre-Louis Bossart

Helpers to set-up back-ends, create platform devices and common IO/block read/write operations

sound/soc/sof/utils.c new file +3 -0
+int sof_bes_setup(struct device *dev, struct snd_sof_dsp_ops *ops,
+		  struct snd_soc_dai_link *links, int link_num,
+		  struct snd_soc_card *card)

sound/soc/sof/utils.c line 29

Quoted 10 lines
sound/soc/sof/utils.c new file +10 -0
+{
+	char name[32];
+	int i;
+
+	if (!ops || !links || !card)
+		return -EINVAL;
+
+	/* set up BE dai_links */
+	for (i = 0; i < link_num; i++) {
+		snprintf(name, 32, "NoCodec-%d", i);

sizeof(name) ?

yes

Quoted 5 lines

sound/soc/sof/utils.c line 32

Quoted 4 lines
sound/soc/sof/utils.c new file +3 -0
+		links[i].name = devm_kstrdup(dev, name, GFP_KERNEL);
+		if (!links[i].name)
+			return -ENOMEM;

...or better devm_kasprintf().

and yes

Quoted 24 lines
Quoted 24 lines
sound/soc/sof/utils.c new file +23 -0
+
+		links[i].id = i;
+		links[i].no_pcm = 1;
+		links[i].cpu_dai_name = ops->drv[i].name;
+		links[i].platform_name = "sof-audio";
+		links[i].codec_dai_name = "snd-soc-dummy-dai";
+		links[i].codec_name = "snd-soc-dummy";
+		links[i].dpcm_playback = 1;
+		links[i].dpcm_capture = 1;
+	}
+
+	card->dai_link = links;
+	card->num_links = link_num;
+
+	return 0;
+}
+EXPORT_SYMBOL(sof_bes_setup);
+
+/* register sof platform device */
+int sof_create_platform_device(struct sof_platform_priv *priv)
+{
+	struct snd_sof_pdata *sof_pdata = priv->sof_pdata;
+	struct device *dev = sof_pdata->dev;
Quoted 8 lines

sound/soc/sof/utils.c line 62

Quoted 7 lines
sound/soc/sof/utils.c new file +7 -0
+
+	priv->pdev_pcm =
+		platform_device_register_data(dev, "sof-audio", -1,
+					      sof_pdata, sizeof(*sof_pdata));
+	if (IS_ERR(priv->pdev_pcm)) {
+		dev_err(dev, "error: cannot register device sof-audio. Error %d\n",
+			(int)PTR_ERR(priv->pdev_pcm));

Instead of casting perhaps use %ld?

yes

Quoted 16 lines
Quoted 4 lines
sound/soc/sof/utils.c new file +3 -0
+		return PTR_ERR(priv->pdev_pcm);
+	}
+

sound/soc/sof/utils.c line 92

Quoted 10 lines
sound/soc/sof/utils.c new file +10 -0
+	return 0;
+}
+EXPORT_SYMBOL(sof_create_platform_device);
+void sof_io_write64(struct snd_sof_dev *sdev, void __iomem *addr, u64 value)
+{
+#ifdef CONFIG_64BIT
+	writeq(value, addr);
+#else
+	memcpy_toio(addr, &value, sizeof(value));
+#endif

This ifdef sounds strange.
Just include io64-nonatomic-lo-hi.h and use readq() w/o limitations.

ah yes. We added readq/writeq following your initial feedback, which broke ARCH=i386 and that was added as a workaround.

Quoted 16 lines
Quoted 4 lines
sound/soc/sof/utils.c new file +3 -0
+}
+EXPORT_SYMBOL(sof_io_write64);
+

sound/soc/sof/utils.c line 105

Quoted 10 lines
sound/soc/sof/utils.c new file +10 -0
+u64 sof_io_read64(struct snd_sof_dev *sdev, void __iomem *addr)
+{
+#ifdef CONFIG_64BIT
+	return readq(addr);
+#else
+	u64 val;
+
+	memcpy_fromio(&val, addr, sizeof(val));
+	return val;
+#endif

Ditto.

Quoted 14 lines

sound/soc/sof/utils.c line 161

Quoted 13 lines
sound/soc/sof/utils.c new file +13 -0
+}
+EXPORT_SYMBOL(sof_io_read64);
+		affected_mask = (1 << (8 * n)) - 1;
+
+		/* first read the 32bit data of dest, then change affected
+		 * bytes, and write back to dest. For unaffected bytes, it
+		 * should not be changed
+		 */
+		__ioread32_copy(&tmp, dest + m * 4, 1);
+		tmp &= ~affected_mask;
+
+		tmp |= *(u32 *)(src_byte + m * 4) & affected_mask;
+		__iowrite32_copy(dest + m * 4, &tmp, 1);

Ain't these equivalents to simple ioread32() / iowrite32() ?

They are. We removed a loop but the this can be further simplified indeed. Thanks for finding this.

Quoted 1 line