Skip to contentSkip to conversation

ALSA: Add driver for SGI RAD1 audio

PATCH New Imre Kaloz 2 participants 2 messages Oct 26 Sound

lore
Share & tools
lore

Imre Kaloz

author
#0 lore raw

PATCH 1/1 ALSA: Add driver for SGI RAD1 audio

The RAD1 is the SGI Octane's PCI audio controller. Add a driver for analog and AES3 I/O, AES3 only on digital-only boards. It binds every 10a9:0005 device; only the Octane's built-in RAD1 is tested.

Playback DMA saturates samples not sign-extended from bit 23; it plays a sign-extended copy.

---
 MAINTAINERS        |   6 +
 sound/pci/Kconfig  |  10 +
 sound/pci/Makefile |   2 +
 sound/pci/rad1.c   | 995 +++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 1013 insertions(+)
 create mode 100644 sound/pci/rad1.c
MAINTAINERS +6 -0
index a5c7dbd7529c..37d6310c84ea 100644
@@ -24937,6 +24937,12 @@ F:	include/linux/phylink.h
 F:	include/linux/sfp.h
 K:	phylink\.h|struct\s+phylink|\.phylink|>phylink_|phylink_(autoneg|clear|connect|create|destroy|disconnect|ethtool|helper|mac|mii|of|set|start|stop|test|validate)
 
+SGI RAD1 AUDIO DRIVER
+M:	Imre Kaloz <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	sound/pci/rad1.c
+
 SHARED MEMORY COMMUNICATIONS (SMC) SOCKETS
 M:	D. Wythe <[email protected]>
 M:	Dust Li <[email protected]>
sound/pci/Kconfig +10 -0
index 6366f72b3667..e913d8bfe9a7 100644
@@ -779,6 +779,16 @@ config SND_PCXHR
 	  To compile this driver as a module, choose M here: the module
 	  will be called snd-pcxhr.
 
+config SND_RAD1
+	tristate "SGI RAD1"
+	select SND_PCM
+	help
+	  Say Y here to include support for the SGI RAD1 audio controller
+	  found in SGI Octane workstations.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called snd-rad1.
+
 config SND_RIPTIDE
 	tristate "Conexant Riptide"
 	depends on HAS_IOPORT
sound/pci/Makefile +2 -0
index 9d5e8e12ae73..43551d474f81 100644
@@ -22,6 +22,7 @@ snd-fm801-y := fm801.o
 snd-intel8x0-y := intel8x0.o
 snd-intel8x0m-y := intel8x0m.o
 snd-maestro3-y := maestro3.o
+snd-rad1-y := rad1.o
 snd-rme32-y := rme32.o
 snd-rme96-y := rme96.o
 snd-sis7019-y := sis7019.o
@@ -48,6 +49,7 @@ obj-$(CONFIG_SND_FM801) += snd-fm801.o
 obj-$(CONFIG_SND_INTEL8X0) += snd-intel8x0.o
 obj-$(CONFIG_SND_INTEL8X0M) += snd-intel8x0m.o
 obj-$(CONFIG_SND_MAESTRO3) += snd-maestro3.o
+obj-$(CONFIG_SND_RAD1) += snd-rad1.o
 obj-$(CONFIG_SND_RME32) += snd-rme32.o
 obj-$(CONFIG_SND_RME96) += snd-rme96.o
 obj-$(CONFIG_SND_SIS7019) += snd-sis7019.o
sound/pci/rad1.c new file +995 -0

995 lines not on this page · raw message

base-commit: b3c345c860de8d763a212da8a4d42bf0a509f74e

Signature · 1 line
2.47.3
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] ALSA: rad1: missing buffer and period constraints in snd_rad1_pcm_open() - [Medium] ALSA: rad1…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] ALSA: rad1: missing buffer and period constraints in snd_rad1_pcm_open()
- [Medium] ALSA: rad1: stale audio left in DMA ring by snd_rad1_pcm_resync() --

commit eacc721af59b303627daf14970de1c98326696c7
Author: Imre Kaloz <[email protected]>

ALSA: Add driver for SGI RAD1 audio

This commit adds an ALSA PCI driver for the SGI RAD1 audio controller found in SGI Octane workstations, supporting analog and AES3 audio I/O.

Quoted 5 lines
sound/pci/rad1.c new file +0 -0
new file mode 100644
index 0000000000000..0419b95cd03f1

[ ... ]

Quoted 56 lines
sound/pci/rad1.c new file +56 -0
+/* The running count is the negated number of bytes left in the ring. */
+static unsigned int snd_rad1_hw_pos(struct snd_rad1_pipe *pipe)
+{
+	u32 count = snd_rad1_read(pipe->chip, RAD1_RUN_CTL(pipe->desc->dma));
+
+	return (pipe->buffer_bytes + (count & ~(RAD1_DMA_ALIGN - 1))) %
+	       pipe->buffer_bytes;
+}
+
+/*
+ * A DMA that has not moved by a poll, the first one half a period after
+ * the start, is enabled again, up to RAD1_DMA_START_TRIES times.
+ */
+static void snd_rad1_check_start(struct snd_rad1_pipe *pipe, unsigned int pos)
+{
+	struct snd_rad1 *chip = pipe->chip;
+
+	if (pos || pipe->start_tries == RAD1_DMA_START_TRIES) {
+		pipe->starting = false;
+		if (!pos)
+			dev_warn(pipe->substream->pcm->card->dev,
+				 "%s: DMA did not start\n", pipe->desc->name);
+		return;
+	}
+
+	pipe->start_tries++;
+	guard(spinlock_irqsave)(&chip->lock);
+	if (pipe->running) {
+		snd_rad1_write(chip, pipe->desc->ctl_reg, pipe->ctl);
+		snd_rad1_write_ctl(chip, pipe);
+	}
+}
+
+/* The RAD1 raises no interrupt for DMA progress; poll the position. */
+static enum hrtimer_restart snd_rad1_poll(struct hrtimer *timer)
+{
+	struct snd_rad1_pipe *pipe = container_of(timer, struct snd_rad1_pipe,
+						  timer);
+	unsigned int pos, period;
+
+	if (!READ_ONCE(pipe->running))
+		return HRTIMER_NORESTART;
+
+	pos = snd_rad1_hw_pos(pipe);
+	if (pipe->starting)
+		snd_rad1_check_start(pipe, pos);
+
+	period = pos / pipe->period_bytes;
+	if (period != pipe->period) {
+		pipe->period = period;
+		snd_pcm_period_elapsed(pipe->substream);
+	}
+
+	hrtimer_forward_now(timer, pipe->poll_time);
+	return HRTIMER_RESTART;
+}

[ ... ]

Quoted 2 lines
sound/pci/rad1.c new file +2 -0
+static int snd_rad1_pcm_open(struct snd_pcm_substream *substream)
+{

[ ... ]

Quoted 10 lines
sound/pci/rad1.c new file +10 -0
+	/* samples reach the DMA ring only through snd_rad1_pcm_ack() */
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
+		runtime->hw.info |= SNDRV_PCM_INFO_SYNC_APPLPTR;
+
+	pipe->substream = substream;
+
+	return snd_pcm_hw_constraint_step(runtime, 0,
+					  SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
+					  RAD1_DMA_ALIGN);
+}

[ ... ]

Quoted 13 lines
sound/pci/rad1.c new file +13 -0
+static int snd_rad1_pcm_prepare(struct snd_pcm_substream *substream)
+{
+	struct snd_rad1_pipe *pipe = snd_pcm_substream_chip(substream);
+	struct snd_rad1 *chip = pipe->chip;
+	struct snd_pcm_runtime *runtime = substream->runtime;
+	const struct snd_rad1_pcm_desc *desc = pipe->desc;
+	dma_addr_t addr = runtime->dma_addr;
+	u32 ctl;
+
+	pipe->buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
+	pipe->period_bytes = snd_pcm_lib_period_bytes(substream);
+	pipe->period = 0;
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {

sound/pci/rad1.c line 517

Quoted 10 lines
sound/pci/rad1.c new file +10 -0
+		addr = pipe->ring.addr;
+		memset(pipe->ring.area, 0, pipe->buffer_bytes);
+		memset(&pipe->rec, 0, sizeof(pipe->rec));
+		pipe->rec.hw_buffer_size = pipe->buffer_bytes;
+		pipe->rec.sw_buffer_size = pipe->buffer_bytes;
+	}
+	/* poll twice per period */
+	pipe->poll_time = ns_to_ktime(div_u64((u64)runtime->period_size *
+					      NSEC_PER_SEC, runtime->rate * 2));
+	ctl = (-pipe->buffer_bytes & ~(RAD1_DMA_ALIGN - 1)) | desc->dma << 3;

[Severity: High]
In sound/pci/rad1.c, snd_rad1_pcm_open() constrains SNDRV_PCM_HW_PARAM_PERIOD_BYTES to a multiple of RAD1_DMA_ALIGN (128), but does not constrain SNDRV_PCM_HW_PARAM_BUFFER_BYTES to RAD1_DMA_ALIGN or constrain SNDRV_PCM_HW_PARAM_PERIODS to an integer.

Because the ALSA PCM core allows non-integer period counts by default, userspace can configure SNDRV_PCM_IOCTL_HW_PARAMS with a period_bytes multiple of 128 and a buffer_bytes that is only a multiple of the 8-byte stereo S24 frame size (for example, period_bytes = 1024 and buffer_bytes = 2056).

When pipe->buffer_bytes is not a multiple of 128, masking the two's-complement negation in snd_rad1_pcm_prepare():

	ctl = (-pipe->buffer_bytes & ~(RAD1_DMA_ALIGN - 1)) | desc->dma << 3;

rounds the negated byte count down to the next 128-byte boundary (e.g., -2056 & ~127 becomes -2176). Does this program the DMA engine to transfer up to 120 bytes past pipe->buffer_bytes in runtime->dma_area on capture or past the cleared region of pipe->ring.area on playback?

In addition, when -(count & ~(RAD1_DMA_ALIGN - 1)) starts at 2176 while pipe->buffer_bytes is 2056, the unsigned addition in snd_rad1_hw_pos():

	return (pipe->buffer_bytes + (count & ~(RAD1_DMA_ALIGN - 1))) %
	       pipe->buffer_bytes;

underflows 32-bit arithmetic (2056 + (-2176) = 0xffffff88U) and returns a non-zero offset before DMA has moved, and then jumps backwards once the first 128-byte block finishes, causing snd_rad1_pcm_pointer() to take the pos < start wrap branch and zero out unplayed samples in pipe->ring.area.

Furthermore, in snd_rad1_poll():

	period = pos / pipe->period_bytes;

doesn't this period boundary check require buffer_bytes to be an integer multiple of period_bytes so that wrap-around does not happen mid-period? Should snd_rad1_pcm_open() also apply snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS) (or constrain SNDRV_PCM_HW_PARAM_BUFFER_BYTES to RAD1_DMA_ALIGN)?

[ ... ]

Quoted 38 lines
sound/pci/rad1.c new file +38 -0
+/* Copy again from the DMA position, which hw_ptr is at. */
+static void snd_rad1_pcm_resync(struct snd_rad1_pipe *pipe,
+				snd_pcm_uframes_t hw_ptr)
+{
+	pipe->rec.appl_ptr = hw_ptr;
+	pipe->rec.hw_data = pipe->rec.hw_io;
+	pipe->rec.sw_data = pipe->rec.sw_io;
+	pipe->rec.hw_ready = 0;
+}
+
+static int snd_rad1_pcm_ack(struct snd_pcm_substream *substream)
+{
+	struct snd_rad1_pipe *pipe = snd_pcm_substream_chip(substream);
+	struct snd_pcm_runtime *runtime = substream->runtime;
+	struct snd_pcm_indirect *rec = &pipe->rec;
+	snd_pcm_uframes_t appl_ptr = runtime->control->appl_ptr;
+	snd_pcm_uframes_t frames;
+
+	if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
+		return 0;
+
+	/* a rewind moves appl_ptr back over samples already in the ring */
+	frames = appl_ptr - rec->appl_ptr;
+	if (appl_ptr < rec->appl_ptr)
+		frames += runtime->boundary;
+	if (frames > runtime->buffer_size)
+		snd_rad1_pcm_resync(pipe, runtime->status->hw_ptr);
+
+	return snd_pcm_indirect_playback_transfer(substream, rec,
+						  snd_rad1_pcm_copy);
+}
+
+/* A reset moves appl_ptr to hw_ptr without calling .ack. */
+static int snd_rad1_pcm_ioctl(struct snd_pcm_substream *substream,
+			      unsigned int cmd, void *arg)
+{
+	struct snd_rad1_pipe *pipe = snd_pcm_substream_chip(substream);
+	int err = snd_pcm_lib_ioctl(substream, cmd, arg);

sound/pci/rad1.c line 659

Quoted 10 lines
sound/pci/rad1.c new file +10 -0
+
+	if (err || cmd != SNDRV_PCM_IOCTL1_RESET ||
+	    substream->stream == SNDRV_PCM_STREAM_CAPTURE)
+		return err;
+
+	guard(pcm_stream_lock_irqsave)(substream);
+	snd_rad1_pcm_resync(pipe, substream->runtime->status->hw_ptr);
+
+	return 0;
+}

[Severity: Medium]
When a playback substream is reset via SNDRV_PCM_IOCTL1_RESET in snd_rad1_pcm_ioctl() or rewound via snd_rad1_pcm_ack(), snd_rad1_pcm_resync() resets pipe->rec.hw_data to pipe->rec.hw_io and sets pipe->rec.hw_ready = 0.

However, the samples that were previously copied into pipe->ring.area ahead of hw_io are left in the DMA ring buffer, and pipe->rec.sw_ready is not cleared. On a rewind, snd_pcm_indirect_playback_transfer() only overwrites pipe->ring.area up to the new rewound appl_ptr, leaving the rewound tail in pipe->ring.area; on a reset, none of the unplayed region in pipe->ring.area is cleared.

Since snd_rad1_pcm_pointer() only zeroes regions of pipe->ring.area after the hardware DMA pointer advances past them, could the DMA engine play those stale, discarded samples from pipe->ring.area instead of silence if the stream underruns, drains, or continues running after a reset or rewind?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1