media: hws: add HDMI audio capture support
Subsystems (2)
[email protected]
maintainerv3 cover media: hws: add HDMI audio capture support
From: Ben Hoff <[email protected]>
This series prepares the AVMatrix HWS PCIe capture driver for HDMI audio capture by making the video DMA programming use the hardware BAR remap windows consistently, adding the shared scratch DMA arena required by the remap model, adding a video bounce path for the shared remap window, hardening queue ownership, and wiring up ALSA PCM capture support.
This driver is derived from a GPL out-of-tree driver. The baseline used for comparison is available at:
https://github.com/benhoff/hws/tree/baseline
This series supersedes the earlier standalone submission at:
https://patchwork.linuxtv.org/project/linux-media/patch/[email protected]/
Supersedes: <[email protected]>
Validation:
- Build checks passed with W=1 for the HWS driver as a module (VIDEO_HWS=m) and through the built-in directory target (VIDEO_HWS=y, producing drivers/media/pci/hws/built-in.a). C=1 builds completed for both configurations
- v4l2-compliance 1.32.0, 64 bits, 64-bit time_t, passed
Total: 51, Succeeded: 51, Failed: 0, Warnings: 1
- HDMI audio progressive validation passed. Coverage included ALSA capture-device enumeration, advertised hardware-parameter checks, 48 kHz S16_LE stereo capture from an HDMI playback, WAV header/size/nonzero-sample checks, captured rate/channel/format verification, no reported XRUNs, no kernel warnings/errors during capture, repeated start/stop capture cycles, 100 consecutive short captures, and 1000 consecutive device open/close cycles.
- ALSA period/buffer matrix validation passed at 48 kHz S16_LE stereo with HDMI playback tone. Tested period/buffer frame pairs were 256/1024, 256/4096, 1024/4096, and 4096/16384; each case checked that arecord accepted the requested hw params, capture completed, the WAV artifact had valid header/size/non-silent content and the expected rate/channel/format, no XRUN was reported, and no kernel warning/error appeared during the case.
- Mixed audio/video validation passed. V4L2 MMAP streaming was kept active on an HWS video node while ALSA captured 48 kHz S16_LE stereo audio from the matching HWS PCM device. The test checked that audio capture completed, video streaming stayed active for the full run, the WAV artifact was present, non-truncated, and non-silent, and the kernel log delta did not contain warnings, errors, DMA faults, XRUNs, overrun/underrun reports, or HWS timeout/stall diagnostics.
- video tested with enabled_audio=0
- pm_test=devices passes with and without D3hot.
Full platform S3/deep suspend is not considered validated, my system is unstable that I can't resume for deep suspend even without the driver
Changes since v2:
- Add missing Signed-off-by tags to patches 1, 3, 4, and 5.
- Wrap the long commit-message paragraph in patch 2.
- Add commit-message descriptions to patches 3, 4, and 5.
- No code changes.
Changes since v1:
- Repost as a properly split patch series. The previous posting was sent as a single cover-letter email with patches appended.
- No code changes.
Ben Hoff (5):
media: hws: program video DMA through remap windows
media: hws: add shared scratch DMA arena
media: hws: add video bounce path for shared remap windows
media: hws: harden video DMA queue ownership
media: hws: add HDMI audio capture support
Signature · 1 line
2.54.0
[email protected]
maintainerPATCH v3 1/5 media: hws: program video DMA through remap windows
From: Ben Hoff <[email protected]>
Video capture DMA setup uses the BAR remap table plus per-channel device buffer offsets. Drop the stale direct writes to the legacy DMA address register and share the remap slot offset between probe-time seeding and runtime programming.
--- drivers/media/pci/hws/hws_irq.c | 6 ++---- drivers/media/pci/hws/hws_pci.c | 11 +++++------ drivers/media/pci/hws/hws_reg.h | 11 ++++++----- drivers/media/pci/hws/hws_video.c | 31 +++++++------------------------ drivers/media/pci/hws/hws_video.h | 2 -- 5 files changed, 20 insertions(+), 41 deletions(-)
drivers/media/pci/hws/hws_irq.c +2 -4
@@ -60,7 +60,7 @@ static int hws_arm_next(struct hws_pcie_dev *hws, u32 ch)dev_dbg (&hws->pdev->dev,"arm_next(ch=%u): picked buffer %p\n" , ch, buf); -/* Publish descriptor(s) before doorbell/MMIO kicks. */ +/* Publish descriptor(s) before MMIO capture updates. */ wmb ();/* Avoid MMIO during suspend */
@@ -79,13 +79,11 @@ static int hws_arm_next(struct hws_pcie_dev *hws, u32 ch)return -EBUSY ; } -/* Also program the DMA address register directly */ +/* Program the video DMA window for the selected buffer. */ {dma_addr_t dma_addr =vb2_dma_contig_plane_dma_addr (&buf->vb.vb2_buf,0 );hws_program_dma_for_addr (hws, ch, dma_addr); -iowrite32 (lower_32_bits (dma_addr), - hws->bar0_base +HWS_REG_DMA_ADDR (ch)); } dev_dbg(&hws->pdev->dev, "arm_next(ch=%u): programmed buffer %p\n", ch,
drivers/media/pci/hws/hws_pci.c +5 -6
@@ -315,12 +315,11 @@ static void hws_seed_channel(struct hws_pcie_dev *hws, int ch) lo &=PCI_E_BAR_ADD_MASK ; -/* Program 64-bit BAR remap entry for this channel (table @ 0x208 + ch * 8) */ -writel_relaxed (hi, hws->bar0_base + -PCI_ADDR_TABLE_BASE +0x208 + ch *8 ); -writel_relaxed (lo, hws->bar0_base + -PCI_ADDR_TABLE_BASE +0x208 + ch *8 + -PCIE_BARADDROFSIZE ); +/* Program 64-bit BAR remap entry for this channel. */ +writel_relaxed (hi, hws->bar0_base +PCI_ADDR_TABLE_BASE + +HWS_VIDEO_REMAP_SLOT_OFF (ch)); +writel_relaxed (lo, hws->bar0_base +PCI_ADDR_TABLE_BASE + +HWS_VIDEO_REMAP_SLOT_OFF (ch) +PCIE_BARADDROFSIZE );/* Program capture engine per-channel base/half */ writel_relaxed((ch + 1) * PCIEBAR_AXI_BASE + pci_addr,
drivers/media/pci/hws/hws_reg.h +6 -5
@@ -87,11 +87,12 @@#define HWS_REG_HDCP_STATUS (CVBS_IN_BASE + 8 * PCIE_BARADDROFSIZE)#define HWS_REG_DMA_MAX_SIZE (CVBS_IN_BASE + 9 * PCIE_BARADDROFSIZE) -/* Buffer addresses (written once during init/reset). */ -/* Base of host-visible buffer. */ -#define HWS_REG_VBUF1_ADDR (CVBS_IN_BASE + 25 * PCIE_BARADDROFSIZE) -/* Per-channel DMA address. */ -#define HWS_REG_DMA_ADDR (ch) (CVBS_IN_BASE + (26 + (ch)) * PCIE_BARADDROFSIZE) +/* +* Video DMA setup uses one BAR remap-table slot per capture channel. The +* remap-table slot supplies the host DMA page, while CVBS_IN_BUF_BASE + +* ch * 4 supplies the device-side buffer offset within that page. +*/ +#define HWS_VIDEO_REMAP_SLOT_OFF (ch) (0x208 + ((ch) * 8))/* Per-channel live buffer toggles (read-only). */ #define HWS_REG_VBUF_TOGGLE (ch) (CVBS_IN_BASE + (32 + (ch)) * PCIE_BARADDROFSIZE)
drivers/media/pci/hws/hws_video.c +7 -24
@@ -26,7 +26,6 @@#include "hws_irq.h" #include "hws_v4l2_ioctl.h" -#define HWS_REMAP_SLOT_OFF (ch) (0x208 + (ch) * 8)/* one 64-bit slot per ch */ #define HWS_BUF_BASE_OFF (ch) (CVBS_IN_BUF_BASE + (ch) * PCIE_BARADDROFSIZE)#define HWS_HALF_SZ_OFF (ch) (CVBS_IN_BUF_BASE2 + (ch) * PCIE_BARADDROFSIZE)
@@ -59,21 +58,13 @@ module_param_named(dma_window_verify, dma_window_verify, bool, 0644);MODULE_PARM_DESC (dma_window_verify,"Read back DMA window registers after programming (debug)" ); -void hws_set_dma_doorbell (struct hws_pcie_dev *hws,unsigned int ch, -dma_addr_t dma,const char *tag) -{ -iowrite32 (lower_32_bits (dma), hws->bar0_base +HWS_REG_DMA_ADDR (ch)); -dev_dbg (&hws->pdev->dev,"dma_doorbell ch%u: dma=0x%llx tag=%s\n" , ch, - (u64 )dma, tag ? tag :"" ); -} -static void hws_program_dma_window (struct hws_video *vid,dma_addr_t dma) {const u32 addr_mask =PCI_E_BAR_ADD_MASK ;const u32 addr_low_mask =PCI_E_BAR_ADD_LOWMASK ;struct hws_pcie_dev *hws = vid->parent;unsigned int ch = vid->channel_index; -u32 table_off =HWS_REMAP_SLOT_OFF (ch); +u32 table_off =HWS_VIDEO_REMAP_SLOT_OFF (ch);u32 lo =lower_32_bits (dma);u32 hi =upper_32_bits (dma);u32 pci_addr = lo & addr_low_mask;
@@ -170,8 +161,6 @@ void hws_prime_next_locked(struct hws_video *vid) vid->next_prepared = next; dma =vb2_dma_contig_plane_dma_addr (&next->vb.vb2_buf,0 );hws_program_dma_for_addr (hws, vid->channel_index, dma); -iowrite32 (lower_32_bits (dma), - hws->bar0_base +HWS_REG_DMA_ADDR (vid->channel_index));dev_dbg (&hws->pdev->dev,"ch%u pre-armed next buffer %p dma=0x%llx\n" , vid->channel_index, next, (u64 )dma);
@@ -183,7 +172,7 @@ static bool hws_force_no_signal_frame(struct hws_video *v, const char *tag)unsigned long flags;struct hwsvideo_buffer *buf =NULL , *next =NULL ;bool have_next = false; -bool doorbell = false; +bool programmed = false;if (!v)return false;
@@ -239,12 +228,11 @@ static bool hws_force_no_signal_frame(struct hws_video *v, const char *tag)if (have_next && next) {dma_addr_t dma =vb2_dma_contig_plane_dma_addr (&next->vb.vb2_buf,0 ); +hws_program_dma_for_addr (hws, v->channel_index, dma); -hws_set_dma_doorbell (hws, v->channel_index, dma, - tag ? tag :"nosignal_zero" ); - doorbell = true; + programmed = true; } -if (doorbell) { +if (programmed) {wmb ();/* ensure descriptors visible before enabling capture */ hws_enable_video_capture (hws, v->channel_index, true); }
@@ -534,7 +522,6 @@ static void hws_seed_dma_windows(struct hws_pcie_dev *hws) {const u32 addr_mask =PCI_E_BAR_ADD_MASK ;const u32 addr_low_mask =PCI_E_BAR_ADD_LOWMASK ; -u32 table =0x208 ;/* one 64-bit entry per channel */ unsigned int ch;if (!hws || !hws->bar0_base)
@@ -544,7 +531,7 @@ static void hws_seed_dma_windows(struct hws_pcie_dev *hws)if (!hws->cur_max_video_ch || hws->cur_max_video_ch > hws->max_channels) hws->cur_max_video_ch = hws->max_channels; -for (ch =0 ; ch < hws->cur_max_video_ch; ch++, table +=8 ) { +for (ch =0 ; ch < hws->cur_max_video_ch; ch++) {if (!hws->scratch_vid[ch].cpu)continue ;
@@ -554,6 +541,7 @@ static void hws_seed_dma_windows(struct hws_pcie_dev *hws)u32 lo =lower_32_bits (p) & addr_mask;u32 hi =upper_32_bits (p);u32 pci_addr_low =lower_32_bits (p) & addr_low_mask; +u32 table =HWS_VIDEO_REMAP_SLOT_OFF (ch); writel_relaxed(hi, hws->bar0_base + PCI_ADDR_TABLE_BASE +
@@ -1128,8 +1116,6 @@ static void hws_buffer_queue(struct vb2_buffer *vb) dma_addr =vb2_dma_contig_plane_dma_addr (&buf->vb.vb2_buf,0 );hws_program_dma_for_addr (vid->parent, vid->channel_index, dma_addr); -iowrite32 (lower_32_bits (dma_addr), - hws->bar0_base +HWS_REG_DMA_ADDR (vid->channel_index));wmb ();/* ensure descriptors visible before enabling capture */ hws_enable_video_capture (hws, vid->channel_index, true);
@@ -1214,9 +1200,6 @@ static int hws_start_streaming(struct vb2_queue *q, unsigned int count) dma_addr =vb2_dma_contig_plane_dma_addr (prog_vb2,0 );hws_program_dma_for_addr (hws, v->channel_index, dma_addr); -iowrite32 (lower_32_bits (dma_addr), - hws->bar0_base + -HWS_REG_DMA_ADDR (v->channel_index)); dev_dbg(&hws->pdev->dev, "start_streaming: ch=%u programmed buffer %p dma=0x%08x\n", v->channel_index, to_program,
drivers/media/pci/hws/hws_video.h +0 -2
@@ -20,8 +20,6 @@ void hws_init_video_sys(struct hws_pcie_dev *hws, bool enable);void hws_program_dma_for_addr (struct hws_pcie_dev *hws,unsigned int ch,dma_addr_t dma); -void hws_set_dma_doorbell (struct hws_pcie_dev *hws,unsigned int ch, -dma_addr_t dma,const char *tag);int hws_video_quiesce (struct hws_pcie_dev *hws,const char *reason);void hws_video_pm_resume (struct hws_pcie_dev *hws);
Signature · 1 line
2.54.0
[email protected]
maintainerPATCH v3 2/5 media: hws: add shared scratch DMA arena
In reply to [email protected]From: Ben Hoff <[email protected]>
Replace the probe-time seed buffer allocation with a per-channel coherent scratch arena that is acquired by stream users and released by refcount. The arena reserves video bounce space first and an audio capture window after it, and validates that the full allocation fits within the single 512 MiB BAR remap page shared by the channel.
Reset cached video DMA window state when freeing a channel arena so the next stream reprograms the hardware from a clean state.
--- drivers/media/pci/hws/hws.h | 28 +++++ drivers/media/pci/hws/hws_pci.c | 192 ++++++++++++++++++++++++------ drivers/media/pci/hws/hws_reg.h | 7 ++ drivers/media/pci/hws/hws_video.c | 17 +-- 4 files changed, 198 insertions(+), 46 deletions(-)
drivers/media/pci/hws/hws.h +28 -0
@@ -8,6 +8,7 @@#include <linux/kthread.h> #include <linux/pci.h> #include <linux/list.h> +#include <linux/mutex.h> #include <linux/spinlock.h> #include <linux/sizes.h> #include <linux/atomic.h>
@@ -52,6 +53,8 @@ struct hwsvideo_buffer {int slot; }; +#define HWS_VIDEO_BOUNCE_SLOTS 2 +struct hws_video {/* Linkage */ struct hws_pcie_dev *parent;
@@ -104,6 +107,7 @@ struct hws_video {u32 last_dma_page;u32 last_pci_addr;u32 last_half16; +u8 next_bounce_slot;/* Misc counters */ int signal_loss_cnt;
@@ -130,6 +134,7 @@ struct hws_scratch_dma {void *cpu;dma_addr_t dma;size_t size; +bool owned; };struct hws_pcie_dev {
@@ -152,6 +157,7 @@ struct hws_pcie_dev {u32 max_hw_video_buf_sz;u8 max_channels;u8 cur_max_video_ch; +u8 cur_max_audio_ch;bool start_run;bool buf_allocated;
@@ -161,7 +167,10 @@ struct hws_pcie_dev {/* Kernel thread */ struct task_struct *main_task; +struct mutex scratch_lock;/* protects scratch DMA arenas and user refs */ +unsigned int scratch_users[MAX_VID_CHANNELS ];struct hws_scratch_dma scratch_vid[MAX_VID_CHANNELS ]; +struct hws_scratch_dma scratch_aud[MAX_VID_CHANNELS ];bool suspended;int irq;
@@ -170,4 +179,23 @@ struct hws_pcie_dev { int pci_lost; }; +static inline bool hws_dma_fits_remap_window (dma_addr_t dma,size_t size) +{ +dma_addr_t end; + +if (!size) +return false; + + end = dma + size -1 ; +if (end < dma) +return false; + +return upper_32_bits (dma) ==upper_32_bits (end) && + (lower_32_bits (dma) &PCI_E_BAR_ADD_MASK ) == + (lower_32_bits (end) &PCI_E_BAR_ADD_MASK ); +} + +int hws_alloc_channel_scratch (struct hws_pcie_dev *hws,unsigned int ch); +void hws_release_channel_scratch (struct hws_pcie_dev *hws,unsigned int ch); +#endif
drivers/media/pci/hws/hws_pci.c +153 -39
265 lines not on this page · raw message
drivers/media/pci/hws/hws_reg.h +7 -0
@@ -33,6 +33,13 @@#define PCI_E_BAR_ADD_MASK 0xE0000000#define PCI_E_BAR_ADD_LOWMASK 0x1FFFFFFF +/* +* The legacy driver reserved a 10 KiB hardware capture window per audio +* channel even though the delivered packet size is smaller. Keep that headroom +* for the split-buffer DMA engine. +*/ +#define MAX_AUDIO_CAP_SIZE (10U * 1024U) +#define MAX_VID_CHANNELS 4#define MAX_MM_VIDEO_SIZE SZ_4M
drivers/media/pci/hws/hws_video.c +10 -7
@@ -306,6 +306,7 @@ int hws_video_init_channel(struct hws_pcie_dev *pdev, int ch) vid->queued_count =0 ; vid->window_valid = false; + vid->next_bounce_slot =0 ;/* Default format. */ vid->pix.width =1920 ;
@@ -556,17 +557,18 @@ static void hws_seed_dma_windows(struct hws_pcie_dev *hws) hws->bar0_base +CVBS_IN_BUF_BASE + ch * PCIE_BARADDROFSIZE); -/* Half-frame length in /16 units. -* Prefer the current channel's computed half_size if available. -* Fall back to half of the probe-owned scratch buffer. +/* +* Half-frame length in /16 units. Prefer the current +* format and fall back to the video bounce window, +* not the full per-channel arena that also contains audio. */ {u32 half_bytes = hws->video[ch].pix.half_size ? - hws->video[ch].pix.half_size : - (u32)(hws->scratch_vid[ch].size /2 ); + hws->video[ch].pix.half_size : + (u32)(MAX_VIDEO_SCALER_SIZE /2 ); +writel_relaxed (half_bytes /16 , - hws->bar0_base + -CVBS_IN_BUF_BASE2 + + hws->bar0_base +CVBS_IN_BUF_BASE2 + ch *PCIE_BARADDROFSIZE ); } }
@@ -1166,6 +1168,7 @@ static int hws_start_streaming(struct vb2_queue *q, unsigned int count) }return ret; } + (void )hws_read_active_state (hws, v->channel_index, &v->pix.interlaced);
Signature · 1 line
2.54.0
Re: [PATCH v3 2/5] media: hws: add shared scratch DMA arena
Hi Ben,
Quoted 10 lines · On 29/06/2026 20:26, [email protected] wrote:
From: Ben Hoff <[email protected]>
Replace the probe-time seed buffer allocation with a per-channel coherent scratch arena that is acquired by stream users and released by refcount. The arena reserves video bounce space first and an audio capture window after it, and validates that the full allocation fits within the single 512 MiB BAR remap page shared by the channel.
Reset cached video DMA window state when freeing a channel arena so the next stream reprograms the hardware from a clean state.
This patch is marked as 'Changes Requested' in patchwork:
https://patchwork.linuxtv.org/project/linux-media/patch/[email protected]/
I suspect that's a mistake. Can you confirm that the patchwork state should really be 'New'? Or are there really more changes coming and I just missed something?
Regards,
Hans
Quoted 428 lines
Signed-off-by Ben Hoff <[email protected]>--- drivers/media/pci/hws/hws.h | 28 +++++ drivers/media/pci/hws/hws_pci.c | 192 ++++++++++++++++++++++++------ drivers/media/pci/hws/hws_reg.h | 7 ++ drivers/media/pci/hws/hws_video.c | 17 +-- 4 files changed, 198 insertions(+), 46 deletions(-)drivers/media/pci/hws/hws.h +28
-0@@ -8,6 +8,7 @@#include <linux/kthread.h> #include <linux/pci.h> #include <linux/list.h> +#include <linux/mutex.h> #include <linux/spinlock.h> #include <linux/sizes.h> #include <linux/atomic.h> @@ -52,6 +53,8 @@ struct hwsvideo_buffer {int slot; }; +#define HWS_VIDEO_BOUNCE_SLOTS 2 +struct hws_video {/* Linkage */ struct hws_pcie_dev *parent;@@ -104,6 +107,7 @@ struct hws_video {u32 last_dma_page;u32 last_pci_addr;u32 last_half16; +u8 next_bounce_slot;/* Misc counters */ int signal_loss_cnt;@@ -130,6 +134,7 @@ struct hws_scratch_dma {void *cpu;dma_addr_t dma;size_t size; +bool owned; };struct hws_pcie_dev {@@ -152,6 +157,7 @@ struct hws_pcie_dev {u32 max_hw_video_buf_sz;u8 max_channels;u8 cur_max_video_ch; +u8 cur_max_audio_ch;bool start_run;bool buf_allocated;@@ -161,7 +167,10 @@ struct hws_pcie_dev {/* Kernel thread */ struct task_struct *main_task; +struct mutex scratch_lock;/* protects scratch DMA arenas and user refs */ +unsigned int scratch_users[MAX_VID_CHANNELS ];struct hws_scratch_dma scratch_vid[MAX_VID_CHANNELS ]; +struct hws_scratch_dma scratch_aud[MAX_VID_CHANNELS ];bool suspended;int irq;@@ -170,4 +179,23 @@ struct hws_pcie_dev { int pci_lost; }; +static inline bool hws_dma_fits_remap_window (dma_addr_t dma,size_t size) +{ +dma_addr_t end; + +if (!size) +return false; + + end = dma + size -1 ; +if (end < dma) +return false; + +return upper_32_bits (dma) ==upper_32_bits (end) && + (lower_32_bits (dma) &PCI_E_BAR_ADD_MASK ) == + (lower_32_bits (end) &PCI_E_BAR_ADD_MASK ); +} + +int hws_alloc_channel_scratch (struct hws_pcie_dev *hws,unsigned int ch); +void hws_release_channel_scratch (struct hws_pcie_dev *hws,unsigned int ch); +#endif drivers/media/pci/hws/hws_pci.c +153
-39@@ -11,6 +11,7 @@#include <linux/err.h> #include <linux/ktime.h> #include <linux/math64.h> +#include <linux/minmax.h> #include <linux/pm.h> #include <linux/freezer.h> #include <linux/pci_regs.h> @@ -101,22 +102,30 @@ static void hws_configure_hardware_capabilities(struct hws_pcie_dev *hdev)case 0x8504 :case 0x6504 : hdev->cur_max_video_ch =4 ; + hdev->cur_max_audio_ch =4 ;break ;case 0x8532 : hdev->cur_max_video_ch =2 ; + hdev->cur_max_audio_ch =2 ;break ;case 0x8512 :case 0x6502 : hdev->cur_max_video_ch =2 ; + hdev->cur_max_audio_ch =0 ;break ;case 0x8501 : hdev->cur_max_video_ch =1 ; + hdev->cur_max_audio_ch =0 ;break ;default : hdev->cur_max_video_ch =4 ; + hdev->cur_max_audio_ch =0 ;break ; } +if (hdev->cur_max_audio_ch > hdev->cur_max_video_ch) + hdev->cur_max_audio_ch = hdev->cur_max_video_ch; +/* universal buffer capacity */ hdev->max_hw_video_buf_sz =MAX_MM_VIDEO_SIZE ;@@ -255,55 +264,164 @@ static void hws_stop_kthread_action(void *data) } } -static int hws_alloc_seed_buffers (struct hws_pcie_dev *hws) +static size_t hws_video_scratch_bytes (void ) { -int ch; -/* 64 KiB is plenty for a safe dummy; hardware needs 64-byte alignment. */ -const size_t need =ALIGN (64 *1024 ,64 ); +return HWS_VIDEO_BOUNCE_SLOTS *ALIGN ((size_t )MAX_VIDEO_SCALER_SIZE ,64 ); +} -for (ch =0 ; ch < hws->cur_max_video_ch; ch++) { +static size_t hws_audio_scratch_bytes (void ) +{ +return ALIGN ((size_t )MAX_AUDIO_CAP_SIZE ,64 ); +} + +static void hws_clear_scratch (struct hws_scratch_dma *scratch) +{ + scratch->cpu =NULL ; + scratch->dma =0 ; + scratch->size =0 ; + scratch->owned = false; +} + +static void hws_free_channel_scratch_locked (struct hws_pcie_dev *hws, +unsigned int ch) +{ +struct hws_scratch_dma *vid; +struct hws_scratch_dma *aud; + +if (!hws || ch >=MAX_VID_CHANNELS ) +return ; + + vid = &hws->scratch_vid[ch]; + aud = &hws->scratch_aud[ch]; + +if (ch < hws->cur_max_video_ch) { + hws->video[ch].window_valid = false; + hws->video[ch].last_dma_hi =0 ; + hws->video[ch].last_dma_page =0 ; + hws->video[ch].last_pci_addr =0 ; + hws->video[ch].last_half16 =0 ; + hws->video[ch].next_bounce_slot =0 ; + } + hws->scratch_users[ch] =0 ; + +if (aud->cpu && aud->owned) +dma_free_coherent (&hws->pdev->dev, aud->size, aud->cpu, + aud->dma); +hws_clear_scratch (aud); + +if (vid->cpu && vid->owned) +dma_free_coherent (&hws->pdev->dev, vid->size, vid->cpu, + vid->dma); +hws_clear_scratch (vid); +} + +int hws_alloc_channel_scratch (struct hws_pcie_dev *hws,unsigned int ch) +{ +size_t aud_off =hws_video_scratch_bytes (); +size_t arena_need = aud_off; +bool has_audio; + + if (!hws || ch >= max_t(unsigned int, hws->cur_max_video_ch, + hws->cur_max_audio_ch)) +return -EINVAL ; + + has_audio = ch < hws->cur_max_audio_ch; +if (has_audio) + arena_need =ALIGN (aud_off +hws_audio_scratch_bytes (),64 ); + +/* +* One coherent per-channel arena backs both fallback video DMA and audio +* DMA. The video bounce slots live first; the audio capture window starts +* at aud_off. The whole arena must fit inside a single 512 MiB remap page +* because video and audio share the channel remap slot. +*/ +mutex_lock (&hws->scratch_lock); +if (hws->scratch_vid[ch].cpu) { + hws->scratch_users[ch]++; +mutex_unlock (&hws->scratch_lock); +return 0 ; + } + + {#if defined(CONFIG_HAS_DMA )/* normal on PCIe platforms */ -void *cpu =dma_alloc_coherent (&hws->pdev->dev, need, - &hws->scratch_vid[ch].dma, -GFP_KERNEL ); +dma_addr_t dma =0 ; +void *cpu =NULL ; +int attempt; + +for (attempt =0 ; attempt <8 ; attempt++) { + cpu =dma_alloc_coherent (&hws->pdev->dev, arena_need, + &dma,GFP_KERNEL ); +if (!cpu) +break ; +if (!hws_dma_fits_remap_window (dma, arena_need)) { +dev_dbg (&hws->pdev->dev, +"scratch arena: retry ch=%u dma=%pad size=%zu crosses remap window\n" , + ch, &dma, arena_need); +dma_free_coherent (&hws->pdev->dev, arena_need, + cpu, dma); + cpu =NULL ; +continue ; + } +break ; + }#else void *cpu =NULL ; +dma_addr_t dma =0 ;#endif if (!cpu) {dev_warn (&hws->pdev->dev, -"scratch: dma_alloc_coherent failed ch=%d\n" , ch); -/* not fatal: free earlier ones and continue without seeding */ -while (--ch >=0 ) { -if (hws->scratch_vid[ch].cpu) -dma_free_coherent (&hws->pdev->dev, - hws->scratch_vid[ch].size, - hws->scratch_vid[ch].cpu, - hws->scratch_vid[ch].dma); - hws->scratch_vid[ch].cpu =NULL ; - hws->scratch_vid[ch].size =0 ; - } +"scratch arena: dma_alloc_coherent failed ch=%u\n" , + ch); +mutex_unlock (&hws->scratch_lock);return -ENOMEM ; } - hws->scratch_vid[ch].cpu = cpu; - hws->scratch_vid[ch].size = need; + + hws->scratch_vid[ch].dma = dma; + hws->scratch_vid[ch].cpu = cpu; + hws->scratch_vid[ch].size = arena_need; + hws->scratch_vid[ch].owned = true; + +if (has_audio) { + hws->scratch_aud[ch].dma = dma + aud_off; + hws->scratch_aud[ch].cpu = (u8 *)cpu + aud_off; + hws->scratch_aud[ch].size =hws_audio_scratch_bytes (); + hws->scratch_aud[ch].owned = false; + } } + hws->scratch_users[ch] =1 ; + +dev_dbg (&hws->pdev->dev, +"scratch arena: allocated ch=%u size=%zu audio=%d\n" , + ch, arena_need, has_audio); +mutex_unlock (&hws->scratch_lock);return 0 ; } +void hws_release_channel_scratch (struct hws_pcie_dev *hws,unsigned int ch) +{ +if (!hws || ch >=MAX_VID_CHANNELS ) +return ; + +mutex_lock (&hws->scratch_lock); +if (hws->scratch_users[ch] >0 ) + hws->scratch_users[ch]--; +if (!hws->scratch_users[ch]) +hws_free_channel_scratch_locked (hws, ch); +mutex_unlock (&hws->scratch_lock); +} +static void hws_free_seed_buffers (struct hws_pcie_dev *hws) {int ch; -for (ch =0 ; ch < hws->cur_max_video_ch; ch++) { -if (hws->scratch_vid[ch].cpu) { -dma_free_coherent (&hws->pdev->dev, - hws->scratch_vid[ch].size, - hws->scratch_vid[ch].cpu, - hws->scratch_vid[ch].dma); - hws->scratch_vid[ch].cpu =NULL ; - hws->scratch_vid[ch].size =0 ; - } - } +if (!hws) +return ; + +/* Teardown-only force-free path; normal streams use release refcounts. */ +mutex_lock (&hws->scratch_lock); +for (ch =0 ; ch <MAX_VID_CHANNELS ; ch++) +hws_free_channel_scratch_locked (hws, ch); +mutex_unlock (&hws->scratch_lock); }static void hws_seed_channel (struct hws_pcie_dev *hws,int ch)@@ -326,11 +444,11 @@ static void hws_seed_channel(struct hws_pcie_dev *hws, int ch) hws->bar0_base +CVBS_IN_BUF_BASE + ch * PCIE_BARADDROFSIZE); -/* Half size: use either the current format's half or half of scratch. */ +/* Half size: use either the current format or the video arena. */ {u32 half = hws->video[ch].pix.half_size ? hws->video[ch].pix.half_size : - (u32)(hws->scratch_vid[ch].size /2 ); + (u32)(MAX_VIDEO_SCALER_SIZE /2 ); writel_relaxed(half / 16, hws->bar0_base + CVBS_IN_BUF_BASE2 +@@ -400,6 +518,7 @@ static int hws_probe(struct pci_dev *pdev, const struct pci_device_id *pci_id) hws->pdev = pdev; hws->irq =-1 ; hws->suspended = false; +mutex_init (&hws->scratch_lock);pci_set_drvdata (pdev, hws);/* 1) Enable device + bus mastering (managed) */ @@ -448,12 +567,7 @@ static int hws_probe(struct pci_dev *pdev, const struct pci_device_id *pci_id) } } -/* 6) Allocate scratch DMA and seed BAR table + channel base/half (legacy SetDMAAddress) */ - ret =hws_alloc_seed_buffers (hws); -if (!ret) -hws_seed_all_channels (hws); - -/* 7) Start-run sequence. */ +/* 6) Start-run sequence. Scratch DMA is allocated on stream start. */ hws_init_video_sys (hws, false);/* A) Force legacy INTx; legacy used request_irq(pdev->irq, ..., IRQF_SHARED) */ drivers/media/pci/hws/hws_reg.h +7
-0@@ -33,6 +33,13 @@#define PCI_E_BAR_ADD_MASK 0xE0000000#define PCI_E_BAR_ADD_LOWMASK 0x1FFFFFFF +/* +* The legacy driver reserved a 10 KiB hardware capture window per audio +* channel even though the delivered packet size is smaller. Keep that headroom +* for the split-buffer DMA engine. +*/ +#define MAX_AUDIO_CAP_SIZE (10U * 1024U) +#define MAX_VID_CHANNELS 4#define MAX_MM_VIDEO_SIZE SZ_4Mdrivers/media/pci/hws/hws_video.c +10
-7@@ -306,6 +306,7 @@ int hws_video_init_channel(struct hws_pcie_dev *pdev, int ch) vid->queued_count =0 ; vid->window_valid = false; + vid->next_bounce_slot =0 ;/* Default format. */ vid->pix.width =1920 ;@@ -556,17 +557,18 @@ static void hws_seed_dma_windows(struct hws_pcie_dev *hws) hws->bar0_base +CVBS_IN_BUF_BASE + ch * PCIE_BARADDROFSIZE); -/* Half-frame length in /16 units. -* Prefer the current channel's computed half_size if available. -* Fall back to half of the probe-owned scratch buffer. +/* +* Half-frame length in /16 units. Prefer the current +* format and fall back to the video bounce window, +* not the full per-channel arena that also contains audio. */ {u32 half_bytes = hws->video[ch].pix.half_size ? - hws->video[ch].pix.half_size : - (u32)(hws->scratch_vid[ch].size /2 ); + hws->video[ch].pix.half_size : + (u32)(MAX_VIDEO_SCALER_SIZE /2 ); +writel_relaxed (half_bytes /16 , - hws->bar0_base + -CVBS_IN_BUF_BASE2 + + hws->bar0_base +CVBS_IN_BUF_BASE2 + ch *PCIE_BARADDROFSIZE ); } }@@ -1166,6 +1168,7 @@ static int hws_start_streaming(struct vb2_queue *q, unsigned int count) }return ret; } + (void )hws_read_active_state (hws, v->channel_index, &v->pix.interlaced);
Ben Hoff
maintainerRe: [PATCH v3 2/5] media: hws: add shared scratch DMA arena
Hi Hans,
The “Changes Requested” state is correct. I interpreted your July 15 feedback as requiring a new version of the series, and I have additional changes coming across the patchset.
I have a new revision drafted locally, but I want to test it more thoroughly before sending it.
Happy to get feedback on any component of this patchset as well and I can fold that in as well.
Best,
Ben
Quoted 455 lines · On Mon, Jul 27, 2026 at 11:07 AM <[email protected]> wrote:
Hi Ben,
Quoted 10 lines · On 29/06/2026 20:26, [email protected] wrote:
From: Ben Hoff <[email protected]>
Replace the probe-time seed buffer allocation with a per-channel coherent scratch arena that is acquired by stream users and released by refcount. The arena reserves video bounce space first and an audio capture window after it, and validates that the full allocation fits within the single 512 MiB BAR remap page shared by the channel.
Reset cached video DMA window state when freeing a channel arena so the next stream reprograms the hardware from a clean state.
This patch is marked as 'Changes Requested' in patchwork:
https://patchwork.linuxtv.org/project/linux-media/patch/[email protected]/
I suspect that's a mistake. Can you confirm that the patchwork state should really be 'New'? Or are there really more changes coming and I just missed something?
Regards,
Hans
Quoted 428 lines
Signed-off-by Ben Hoff <[email protected]>--- drivers/media/pci/hws/hws.h | 28 +++++ drivers/media/pci/hws/hws_pci.c | 192 ++++++++++++++++++++++++------ drivers/media/pci/hws/hws_reg.h | 7 ++ drivers/media/pci/hws/hws_video.c | 17 +-- 4 files changed, 198 insertions(+), 46 deletions(-)drivers/media/pci/hws/hws.h +28
-0@@ -8,6 +8,7 @@#include <linux/kthread.h> #include <linux/pci.h> #include <linux/list.h> +#include <linux/mutex.h> #include <linux/spinlock.h> #include <linux/sizes.h> #include <linux/atomic.h> @@ -52,6 +53,8 @@ struct hwsvideo_buffer {int slot; }; +#define HWS_VIDEO_BOUNCE_SLOTS 2 +struct hws_video {/* Linkage */ struct hws_pcie_dev *parent;@@ -104,6 +107,7 @@ struct hws_video {u32 last_dma_page;u32 last_pci_addr;u32 last_half16; +u8 next_bounce_slot;/* Misc counters */ int signal_loss_cnt;@@ -130,6 +134,7 @@ struct hws_scratch_dma {void *cpu;dma_addr_t dma;size_t size; +bool owned; };struct hws_pcie_dev {@@ -152,6 +157,7 @@ struct hws_pcie_dev {u32 max_hw_video_buf_sz;u8 max_channels;u8 cur_max_video_ch; +u8 cur_max_audio_ch;bool start_run;bool buf_allocated;@@ -161,7 +167,10 @@ struct hws_pcie_dev {/* Kernel thread */ struct task_struct *main_task; +struct mutex scratch_lock;/* protects scratch DMA arenas and user refs */ +unsigned int scratch_users[MAX_VID_CHANNELS ];struct hws_scratch_dma scratch_vid[MAX_VID_CHANNELS ]; +struct hws_scratch_dma scratch_aud[MAX_VID_CHANNELS ];bool suspended;int irq;@@ -170,4 +179,23 @@ struct hws_pcie_dev { int pci_lost; }; +static inline bool hws_dma_fits_remap_window (dma_addr_t dma,size_t size) +{ +dma_addr_t end; + +if (!size) +return false; + + end = dma + size -1 ; +if (end < dma) +return false; + +return upper_32_bits (dma) ==upper_32_bits (end) && + (lower_32_bits (dma) &PCI_E_BAR_ADD_MASK ) == + (lower_32_bits (end) &PCI_E_BAR_ADD_MASK ); +} + +int hws_alloc_channel_scratch (struct hws_pcie_dev *hws,unsigned int ch); +void hws_release_channel_scratch (struct hws_pcie_dev *hws,unsigned int ch); +#endif drivers/media/pci/hws/hws_pci.c +153
-39@@ -11,6 +11,7 @@#include <linux/err.h> #include <linux/ktime.h> #include <linux/math64.h> +#include <linux/minmax.h> #include <linux/pm.h> #include <linux/freezer.h> #include <linux/pci_regs.h> @@ -101,22 +102,30 @@ static void hws_configure_hardware_capabilities(struct hws_pcie_dev *hdev)case 0x8504 :case 0x6504 : hdev->cur_max_video_ch =4 ; + hdev->cur_max_audio_ch =4 ;break ;case 0x8532 : hdev->cur_max_video_ch =2 ; + hdev->cur_max_audio_ch =2 ;break ;case 0x8512 :case 0x6502 : hdev->cur_max_video_ch =2 ; + hdev->cur_max_audio_ch =0 ;break ;case 0x8501 : hdev->cur_max_video_ch =1 ; + hdev->cur_max_audio_ch =0 ;break ;default : hdev->cur_max_video_ch =4 ; + hdev->cur_max_audio_ch =0 ;break ; } +if (hdev->cur_max_audio_ch > hdev->cur_max_video_ch) + hdev->cur_max_audio_ch = hdev->cur_max_video_ch; +/* universal buffer capacity */ hdev->max_hw_video_buf_sz =MAX_MM_VIDEO_SIZE ;@@ -255,55 +264,164 @@ static void hws_stop_kthread_action(void *data) } } -static int hws_alloc_seed_buffers (struct hws_pcie_dev *hws) +static size_t hws_video_scratch_bytes (void ) { -int ch; -/* 64 KiB is plenty for a safe dummy; hardware needs 64-byte alignment. */ -const size_t need =ALIGN (64 *1024 ,64 ); +return HWS_VIDEO_BOUNCE_SLOTS *ALIGN ((size_t )MAX_VIDEO_SCALER_SIZE ,64 ); +} -for (ch =0 ; ch < hws->cur_max_video_ch; ch++) { +static size_t hws_audio_scratch_bytes (void ) +{ +return ALIGN ((size_t )MAX_AUDIO_CAP_SIZE ,64 ); +} + +static void hws_clear_scratch (struct hws_scratch_dma *scratch) +{ + scratch->cpu =NULL ; + scratch->dma =0 ; + scratch->size =0 ; + scratch->owned = false; +} + +static void hws_free_channel_scratch_locked (struct hws_pcie_dev *hws, +unsigned int ch) +{ +struct hws_scratch_dma *vid; +struct hws_scratch_dma *aud; + +if (!hws || ch >=MAX_VID_CHANNELS ) +return ; + + vid = &hws->scratch_vid[ch]; + aud = &hws->scratch_aud[ch]; + +if (ch < hws->cur_max_video_ch) { + hws->video[ch].window_valid = false; + hws->video[ch].last_dma_hi =0 ; + hws->video[ch].last_dma_page =0 ; + hws->video[ch].last_pci_addr =0 ; + hws->video[ch].last_half16 =0 ; + hws->video[ch].next_bounce_slot =0 ; + } + hws->scratch_users[ch] =0 ; + +if (aud->cpu && aud->owned) +dma_free_coherent (&hws->pdev->dev, aud->size, aud->cpu, + aud->dma); +hws_clear_scratch (aud); + +if (vid->cpu && vid->owned) +dma_free_coherent (&hws->pdev->dev, vid->size, vid->cpu, + vid->dma); +hws_clear_scratch (vid); +} + +int hws_alloc_channel_scratch (struct hws_pcie_dev *hws,unsigned int ch) +{ +size_t aud_off =hws_video_scratch_bytes (); +size_t arena_need = aud_off; +bool has_audio; + + if (!hws || ch >= max_t(unsigned int, hws->cur_max_video_ch, + hws->cur_max_audio_ch)) +return -EINVAL ; + + has_audio = ch < hws->cur_max_audio_ch; +if (has_audio) + arena_need =ALIGN (aud_off +hws_audio_scratch_bytes (),64 ); + +/* +* One coherent per-channel arena backs both fallback video DMA and audio +* DMA. The video bounce slots live first; the audio capture window starts +* at aud_off. The whole arena must fit inside a single 512 MiB remap page +* because video and audio share the channel remap slot. +*/ +mutex_lock (&hws->scratch_lock); +if (hws->scratch_vid[ch].cpu) { + hws->scratch_users[ch]++; +mutex_unlock (&hws->scratch_lock); +return 0 ; + } + + {#if defined(CONFIG_HAS_DMA )/* normal on PCIe platforms */ -void *cpu =dma_alloc_coherent (&hws->pdev->dev, need, - &hws->scratch_vid[ch].dma, -GFP_KERNEL ); +dma_addr_t dma =0 ; +void *cpu =NULL ; +int attempt; + +for (attempt =0 ; attempt <8 ; attempt++) { + cpu =dma_alloc_coherent (&hws->pdev->dev, arena_need, + &dma,GFP_KERNEL ); +if (!cpu) +break ; +if (!hws_dma_fits_remap_window (dma, arena_need)) { +dev_dbg (&hws->pdev->dev, +"scratch arena: retry ch=%u dma=%pad size=%zu crosses remap window\n" , + ch, &dma, arena_need); +dma_free_coherent (&hws->pdev->dev, arena_need, + cpu, dma); + cpu =NULL ; +continue ; + } +break ; + }#else void *cpu =NULL ; +dma_addr_t dma =0 ;#endif if (!cpu) {dev_warn (&hws->pdev->dev, -"scratch: dma_alloc_coherent failed ch=%d\n" , ch); -/* not fatal: free earlier ones and continue without seeding */ -while (--ch >=0 ) { -if (hws->scratch_vid[ch].cpu) -dma_free_coherent (&hws->pdev->dev, - hws->scratch_vid[ch].size, - hws->scratch_vid[ch].cpu, - hws->scratch_vid[ch].dma); - hws->scratch_vid[ch].cpu =NULL ; - hws->scratch_vid[ch].size =0 ; - } +"scratch arena: dma_alloc_coherent failed ch=%u\n" , + ch); +mutex_unlock (&hws->scratch_lock);return -ENOMEM ; } - hws->scratch_vid[ch].cpu = cpu; - hws->scratch_vid[ch].size = need; + + hws->scratch_vid[ch].dma = dma; + hws->scratch_vid[ch].cpu = cpu; + hws->scratch_vid[ch].size = arena_need; + hws->scratch_vid[ch].owned = true; + +if (has_audio) { + hws->scratch_aud[ch].dma = dma + aud_off; + hws->scratch_aud[ch].cpu = (u8 *)cpu + aud_off; + hws->scratch_aud[ch].size =hws_audio_scratch_bytes (); + hws->scratch_aud[ch].owned = false; + } } + hws->scratch_users[ch] =1 ; + +dev_dbg (&hws->pdev->dev, +"scratch arena: allocated ch=%u size=%zu audio=%d\n" , + ch, arena_need, has_audio); +mutex_unlock (&hws->scratch_lock);return 0 ; } +void hws_release_channel_scratch (struct hws_pcie_dev *hws,unsigned int ch) +{ +if (!hws || ch >=MAX_VID_CHANNELS ) +return ; + +mutex_lock (&hws->scratch_lock); +if (hws->scratch_users[ch] >0 ) + hws->scratch_users[ch]--; +if (!hws->scratch_users[ch]) +hws_free_channel_scratch_locked (hws, ch); +mutex_unlock (&hws->scratch_lock); +} +static void hws_free_seed_buffers (struct hws_pcie_dev *hws) {int ch; -for (ch =0 ; ch < hws->cur_max_video_ch; ch++) { -if (hws->scratch_vid[ch].cpu) { -dma_free_coherent (&hws->pdev->dev, - hws->scratch_vid[ch].size, - hws->scratch_vid[ch].cpu, - hws->scratch_vid[ch].dma); - hws->scratch_vid[ch].cpu =NULL ; - hws->scratch_vid[ch].size =0 ; - } - } +if (!hws) +return ; + +/* Teardown-only force-free path; normal streams use release refcounts. */ +mutex_lock (&hws->scratch_lock); +for (ch =0 ; ch <MAX_VID_CHANNELS ; ch++) +hws_free_channel_scratch_locked (hws, ch); +mutex_unlock (&hws->scratch_lock); }static void hws_seed_channel (struct hws_pcie_dev *hws,int ch)@@ -326,11 +444,11 @@ static void hws_seed_channel(struct hws_pcie_dev *hws, int ch) hws->bar0_base +CVBS_IN_BUF_BASE + ch * PCIE_BARADDROFSIZE); -/* Half size: use either the current format's half or half of scratch. */ +/* Half size: use either the current format or the video arena. */ {u32 half = hws->video[ch].pix.half_size ? hws->video[ch].pix.half_size : - (u32)(hws->scratch_vid[ch].size /2 ); + (u32)(MAX_VIDEO_SCALER_SIZE /2 ); writel_relaxed(half / 16, hws->bar0_base + CVBS_IN_BUF_BASE2 +@@ -400,6 +518,7 @@ static int hws_probe(struct pci_dev *pdev, const struct pci_device_id *pci_id) hws->pdev = pdev; hws->irq =-1 ; hws->suspended = false; +mutex_init (&hws->scratch_lock);pci_set_drvdata (pdev, hws);/* 1) Enable device + bus mastering (managed) */ @@ -448,12 +567,7 @@ static int hws_probe(struct pci_dev *pdev, const struct pci_device_id *pci_id) } } -/* 6) Allocate scratch DMA and seed BAR table + channel base/half (legacy SetDMAAddress) */ - ret =hws_alloc_seed_buffers (hws); -if (!ret) -hws_seed_all_channels (hws); - -/* 7) Start-run sequence. */ +/* 6) Start-run sequence. Scratch DMA is allocated on stream start. */ hws_init_video_sys (hws, false);/* A) Force legacy INTx; legacy used request_irq(pdev->irq, ..., IRQF_SHARED) */ drivers/media/pci/hws/hws_reg.h +7
-0@@ -33,6 +33,13 @@#define PCI_E_BAR_ADD_MASK 0xE0000000#define PCI_E_BAR_ADD_LOWMASK 0x1FFFFFFF +/* +* The legacy driver reserved a 10 KiB hardware capture window per audio +* channel even though the delivered packet size is smaller. Keep that headroom +* for the split-buffer DMA engine. +*/ +#define MAX_AUDIO_CAP_SIZE (10U * 1024U) +#define MAX_VID_CHANNELS 4#define MAX_MM_VIDEO_SIZE SZ_4Mdrivers/media/pci/hws/hws_video.c +10
-7@@ -306,6 +306,7 @@ int hws_video_init_channel(struct hws_pcie_dev *pdev, int ch) vid->queued_count =0 ; vid->window_valid = false; + vid->next_bounce_slot =0 ;/* Default format. */ vid->pix.width =1920 ;@@ -556,17 +557,18 @@ static void hws_seed_dma_windows(struct hws_pcie_dev *hws) hws->bar0_base +CVBS_IN_BUF_BASE + ch * PCIE_BARADDROFSIZE); -/* Half-frame length in /16 units. -* Prefer the current channel's computed half_size if available. -* Fall back to half of the probe-owned scratch buffer. +/* +* Half-frame length in /16 units. Prefer the current +* format and fall back to the video bounce window, +* not the full per-channel arena that also contains audio. */ {u32 half_bytes = hws->video[ch].pix.half_size ? - hws->video[ch].pix.half_size : - (u32)(hws->scratch_vid[ch].size /2 ); + hws->video[ch].pix.half_size : + (u32)(MAX_VIDEO_SCALER_SIZE /2 ); +writel_relaxed (half_bytes /16 , - hws->bar0_base + -CVBS_IN_BUF_BASE2 + + hws->bar0_base +CVBS_IN_BUF_BASE2 + ch *PCIE_BARADDROFSIZE ); } }@@ -1166,6 +1168,7 @@ static int hws_start_streaming(struct vb2_queue *q, unsigned int count) }return ret; } + (void )hws_read_active_state (hws, v->channel_index, &v->pix.interlaced);
Re: [PATCH v3 2/5] media: hws: add shared scratch DMA arena
Hi Ben,
Quoted 8 lines · Ben Hoff
Hi Hans,
The “Changes Requested” state is correct. I interpreted your July 15 feedback as requiring a new version of the series, and I have additional changes coming across the patchset.
I have a new revision drafted locally, but I want to test it more thoroughly before sending it.
I didn't see a v4, this is still being worked on?
In the meantime I'll mark this series as 'Changes Requested' and wait for that v4. That way it is out of our queue :-)
Regards,
Hans
Quoted 465 lines
Happy to get feedback on any component of this patchset as well and I can fold that in as well.
Best,
BenQuoted 455 lines · On Mon, Jul 27, 2026 at 11:07 AM <[email protected]> wrote:
Hi Ben,
Quoted 10 lines · On 29/06/2026 20:26, [email protected] wrote:
From: Ben Hoff <[email protected]>
Replace the probe-time seed buffer allocation with a per-channel coherent scratch arena that is acquired by stream users and released by refcount. The arena reserves video bounce space first and an audio capture window after it, and validates that the full allocation fits within the single 512 MiB BAR remap page shared by the channel.
Reset cached video DMA window state when freeing a channel arena so the next stream reprograms the hardware from a clean state.
This patch is marked as 'Changes Requested' in patchwork:
https://patchwork.linuxtv.org/project/linux-media/patch/[email protected]/
I suspect that's a mistake. Can you confirm that the patchwork state should really be 'New'? Or are there really more changes coming and I just missed something?
Regards,
Hans
Quoted 428 lines
Signed-off-by Ben Hoff <[email protected]>--- drivers/media/pci/hws/hws.h | 28 +++++ drivers/media/pci/hws/hws_pci.c | 192 ++++++++++++++++++++++++------ drivers/media/pci/hws/hws_reg.h | 7 ++ drivers/media/pci/hws/hws_video.c | 17 +-- 4 files changed, 198 insertions(+), 46 deletions(-)drivers/media/pci/hws/hws.h +28
-0@@ -8,6 +8,7 @@#include <linux/kthread.h> #include <linux/pci.h> #include <linux/list.h> +#include <linux/mutex.h> #include <linux/spinlock.h> #include <linux/sizes.h> #include <linux/atomic.h> @@ -52,6 +53,8 @@ struct hwsvideo_buffer {int slot; }; +#define HWS_VIDEO_BOUNCE_SLOTS 2 +struct hws_video {/* Linkage */ struct hws_pcie_dev *parent;@@ -104,6 +107,7 @@ struct hws_video {u32 last_dma_page;u32 last_pci_addr;u32 last_half16; +u8 next_bounce_slot;/* Misc counters */ int signal_loss_cnt;@@ -130,6 +134,7 @@ struct hws_scratch_dma {void *cpu;dma_addr_t dma;size_t size; +bool owned; };struct hws_pcie_dev {@@ -152,6 +157,7 @@ struct hws_pcie_dev {u32 max_hw_video_buf_sz;u8 max_channels;u8 cur_max_video_ch; +u8 cur_max_audio_ch;bool start_run;bool buf_allocated;@@ -161,7 +167,10 @@ struct hws_pcie_dev {/* Kernel thread */ struct task_struct *main_task; +struct mutex scratch_lock;/* protects scratch DMA arenas and user refs */ +unsigned int scratch_users[MAX_VID_CHANNELS ];struct hws_scratch_dma scratch_vid[MAX_VID_CHANNELS ]; +struct hws_scratch_dma scratch_aud[MAX_VID_CHANNELS ];bool suspended;int irq;@@ -170,4 +179,23 @@ struct hws_pcie_dev { int pci_lost; }; +static inline bool hws_dma_fits_remap_window (dma_addr_t dma,size_t size) +{ +dma_addr_t end; + +if (!size) +return false; + + end = dma + size -1 ; +if (end < dma) +return false; + +return upper_32_bits (dma) ==upper_32_bits (end) && + (lower_32_bits (dma) &PCI_E_BAR_ADD_MASK ) == + (lower_32_bits (end) &PCI_E_BAR_ADD_MASK ); +} + +int hws_alloc_channel_scratch (struct hws_pcie_dev *hws,unsigned int ch); +void hws_release_channel_scratch (struct hws_pcie_dev *hws,unsigned int ch); +#endif drivers/media/pci/hws/hws_pci.c +153
-39@@ -11,6 +11,7 @@#include <linux/err.h> #include <linux/ktime.h> #include <linux/math64.h> +#include <linux/minmax.h> #include <linux/pm.h> #include <linux/freezer.h> #include <linux/pci_regs.h> @@ -101,22 +102,30 @@ static void hws_configure_hardware_capabilities(struct hws_pcie_dev *hdev)case 0x8504 :case 0x6504 : hdev->cur_max_video_ch =4 ; + hdev->cur_max_audio_ch =4 ;break ;case 0x8532 : hdev->cur_max_video_ch =2 ; + hdev->cur_max_audio_ch =2 ;break ;case 0x8512 :case 0x6502 : hdev->cur_max_video_ch =2 ; + hdev->cur_max_audio_ch =0 ;break ;case 0x8501 : hdev->cur_max_video_ch =1 ; + hdev->cur_max_audio_ch =0 ;break ;default : hdev->cur_max_video_ch =4 ; + hdev->cur_max_audio_ch =0 ;break ; } +if (hdev->cur_max_audio_ch > hdev->cur_max_video_ch) + hdev->cur_max_audio_ch = hdev->cur_max_video_ch; +/* universal buffer capacity */ hdev->max_hw_video_buf_sz =MAX_MM_VIDEO_SIZE ;@@ -255,55 +264,164 @@ static void hws_stop_kthread_action(void *data) } } -static int hws_alloc_seed_buffers (struct hws_pcie_dev *hws) +static size_t hws_video_scratch_bytes (void ) { -int ch; -/* 64 KiB is plenty for a safe dummy; hardware needs 64-byte alignment. */ -const size_t need =ALIGN (64 *1024 ,64 ); +return HWS_VIDEO_BOUNCE_SLOTS *ALIGN ((size_t )MAX_VIDEO_SCALER_SIZE ,64 ); +} -for (ch =0 ; ch < hws->cur_max_video_ch; ch++) { +static size_t hws_audio_scratch_bytes (void ) +{ +return ALIGN ((size_t )MAX_AUDIO_CAP_SIZE ,64 ); +} + +static void hws_clear_scratch (struct hws_scratch_dma *scratch) +{ + scratch->cpu =NULL ; + scratch->dma =0 ; + scratch->size =0 ; + scratch->owned = false; +} + +static void hws_free_channel_scratch_locked (struct hws_pcie_dev *hws, +unsigned int ch) +{ +struct hws_scratch_dma *vid; +struct hws_scratch_dma *aud; + +if (!hws || ch >=MAX_VID_CHANNELS ) +return ; + + vid = &hws->scratch_vid[ch]; + aud = &hws->scratch_aud[ch]; + +if (ch < hws->cur_max_video_ch) { + hws->video[ch].window_valid = false; + hws->video[ch].last_dma_hi =0 ; + hws->video[ch].last_dma_page =0 ; + hws->video[ch].last_pci_addr =0 ; + hws->video[ch].last_half16 =0 ; + hws->video[ch].next_bounce_slot =0 ; + } + hws->scratch_users[ch] =0 ; + +if (aud->cpu && aud->owned) +dma_free_coherent (&hws->pdev->dev, aud->size, aud->cpu, + aud->dma); +hws_clear_scratch (aud); + +if (vid->cpu && vid->owned) +dma_free_coherent (&hws->pdev->dev, vid->size, vid->cpu, + vid->dma); +hws_clear_scratch (vid); +} + +int hws_alloc_channel_scratch (struct hws_pcie_dev *hws,unsigned int ch) +{ +size_t aud_off =hws_video_scratch_bytes (); +size_t arena_need = aud_off; +bool has_audio; + + if (!hws || ch >= max_t(unsigned int, hws->cur_max_video_ch, + hws->cur_max_audio_ch)) +return -EINVAL ; + + has_audio = ch < hws->cur_max_audio_ch; +if (has_audio) + arena_need =ALIGN (aud_off +hws_audio_scratch_bytes (),64 ); + +/* +* One coherent per-channel arena backs both fallback video DMA and audio +* DMA. The video bounce slots live first; the audio capture window starts +* at aud_off. The whole arena must fit inside a single 512 MiB remap page +* because video and audio share the channel remap slot. +*/ +mutex_lock (&hws->scratch_lock); +if (hws->scratch_vid[ch].cpu) { + hws->scratch_users[ch]++; +mutex_unlock (&hws->scratch_lock); +return 0 ; + } + + {#if defined(CONFIG_HAS_DMA )/* normal on PCIe platforms */ -void *cpu =dma_alloc_coherent (&hws->pdev->dev, need, - &hws->scratch_vid[ch].dma, -GFP_KERNEL ); +dma_addr_t dma =0 ; +void *cpu =NULL ; +int attempt; + +for (attempt =0 ; attempt <8 ; attempt++) { + cpu =dma_alloc_coherent (&hws->pdev->dev, arena_need, + &dma,GFP_KERNEL ); +if (!cpu) +break ; +if (!hws_dma_fits_remap_window (dma, arena_need)) { +dev_dbg (&hws->pdev->dev, +"scratch arena: retry ch=%u dma=%pad size=%zu crosses remap window\n" , + ch, &dma, arena_need); +dma_free_coherent (&hws->pdev->dev, arena_need, + cpu, dma); + cpu =NULL ; +continue ; + } +break ; + }#else void *cpu =NULL ; +dma_addr_t dma =0 ;#endif if (!cpu) {dev_warn (&hws->pdev->dev, -"scratch: dma_alloc_coherent failed ch=%d\n" , ch); -/* not fatal: free earlier ones and continue without seeding */ -while (--ch >=0 ) { -if (hws->scratch_vid[ch].cpu) -dma_free_coherent (&hws->pdev->dev, - hws->scratch_vid[ch].size, - hws->scratch_vid[ch].cpu, - hws->scratch_vid[ch].dma); - hws->scratch_vid[ch].cpu =NULL ; - hws->scratch_vid[ch].size =0 ; - } +"scratch arena: dma_alloc_coherent failed ch=%u\n" , + ch); +mutex_unlock (&hws->scratch_lock);return -ENOMEM ; } - hws->scratch_vid[ch].cpu = cpu; - hws->scratch_vid[ch].size = need; + + hws->scratch_vid[ch].dma = dma; + hws->scratch_vid[ch].cpu = cpu; + hws->scratch_vid[ch].size = arena_need; + hws->scratch_vid[ch].owned = true; + +if (has_audio) { + hws->scratch_aud[ch].dma = dma + aud_off; + hws->scratch_aud[ch].cpu = (u8 *)cpu + aud_off; + hws->scratch_aud[ch].size =hws_audio_scratch_bytes (); + hws->scratch_aud[ch].owned = false; + } } + hws->scratch_users[ch] =1 ; + +dev_dbg (&hws->pdev->dev, +"scratch arena: allocated ch=%u size=%zu audio=%d\n" , + ch, arena_need, has_audio); +mutex_unlock (&hws->scratch_lock);return 0 ; } +void hws_release_channel_scratch (struct hws_pcie_dev *hws,unsigned int ch) +{ +if (!hws || ch >=MAX_VID_CHANNELS ) +return ; + +mutex_lock (&hws->scratch_lock); +if (hws->scratch_users[ch] >0 ) + hws->scratch_users[ch]--; +if (!hws->scratch_users[ch]) +hws_free_channel_scratch_locked (hws, ch); +mutex_unlock (&hws->scratch_lock); +} +static void hws_free_seed_buffers (struct hws_pcie_dev *hws) {int ch; -for (ch =0 ; ch < hws->cur_max_video_ch; ch++) { -if (hws->scratch_vid[ch].cpu) { -dma_free_coherent (&hws->pdev->dev, - hws->scratch_vid[ch].size, - hws->scratch_vid[ch].cpu, - hws->scratch_vid[ch].dma); - hws->scratch_vid[ch].cpu =NULL ; - hws->scratch_vid[ch].size =0 ; - } - } +if (!hws) +return ; + +/* Teardown-only force-free path; normal streams use release refcounts. */ +mutex_lock (&hws->scratch_lock); +for (ch =0 ; ch <MAX_VID_CHANNELS ; ch++) +hws_free_channel_scratch_locked (hws, ch); +mutex_unlock (&hws->scratch_lock); }static void hws_seed_channel (struct hws_pcie_dev *hws,int ch)@@ -326,11 +444,11 @@ static void hws_seed_channel(struct hws_pcie_dev *hws, int ch) hws->bar0_base +CVBS_IN_BUF_BASE + ch * PCIE_BARADDROFSIZE); -/* Half size: use either the current format's half or half of scratch. */ +/* Half size: use either the current format or the video arena. */ {u32 half = hws->video[ch].pix.half_size ? hws->video[ch].pix.half_size : - (u32)(hws->scratch_vid[ch].size /2 ); + (u32)(MAX_VIDEO_SCALER_SIZE /2 ); writel_relaxed(half / 16, hws->bar0_base + CVBS_IN_BUF_BASE2 +@@ -400,6 +518,7 @@ static int hws_probe(struct pci_dev *pdev, const struct pci_device_id *pci_id) hws->pdev = pdev; hws->irq =-1 ; hws->suspended = false; +mutex_init (&hws->scratch_lock);pci_set_drvdata (pdev, hws);/* 1) Enable device + bus mastering (managed) */ @@ -448,12 +567,7 @@ static int hws_probe(struct pci_dev *pdev, const struct pci_device_id *pci_id) } } -/* 6) Allocate scratch DMA and seed BAR table + channel base/half (legacy SetDMAAddress) */ - ret =hws_alloc_seed_buffers (hws); -if (!ret) -hws_seed_all_channels (hws); - -/* 7) Start-run sequence. */ +/* 6) Start-run sequence. Scratch DMA is allocated on stream start. */ hws_init_video_sys (hws, false);/* A) Force legacy INTx; legacy used request_irq(pdev->irq, ..., IRQF_SHARED) */ drivers/media/pci/hws/hws_reg.h +7
-0@@ -33,6 +33,13 @@#define PCI_E_BAR_ADD_MASK 0xE0000000#define PCI_E_BAR_ADD_LOWMASK 0x1FFFFFFF +/* +* The legacy driver reserved a 10 KiB hardware capture window per audio +* channel even though the delivered packet size is smaller. Keep that headroom +* for the split-buffer DMA engine. +*/ +#define MAX_AUDIO_CAP_SIZE (10U * 1024U) +#define MAX_VID_CHANNELS 4#define MAX_MM_VIDEO_SIZE SZ_4Mdrivers/media/pci/hws/hws_video.c +10
-7@@ -306,6 +306,7 @@ int hws_video_init_channel(struct hws_pcie_dev *pdev, int ch) vid->queued_count =0 ; vid->window_valid = false; + vid->next_bounce_slot =0 ;/* Default format. */ vid->pix.width =1920 ;@@ -556,17 +557,18 @@ static void hws_seed_dma_windows(struct hws_pcie_dev *hws) hws->bar0_base +CVBS_IN_BUF_BASE + ch * PCIE_BARADDROFSIZE); -/* Half-frame length in /16 units. -* Prefer the current channel's computed half_size if available. -* Fall back to half of the probe-owned scratch buffer. +/* +* Half-frame length in /16 units. Prefer the current +* format and fall back to the video bounce window, +* not the full per-channel arena that also contains audio. */ {u32 half_bytes = hws->video[ch].pix.half_size ? - hws->video[ch].pix.half_size : - (u32)(hws->scratch_vid[ch].size /2 ); + hws->video[ch].pix.half_size : + (u32)(MAX_VIDEO_SCALER_SIZE /2 ); +writel_relaxed (half_bytes /16 , - hws->bar0_base + -CVBS_IN_BUF_BASE2 + + hws->bar0_base +CVBS_IN_BUF_BASE2 + ch *PCIE_BARADDROFSIZE ); } }@@ -1166,6 +1168,7 @@ static int hws_start_streaming(struct vb2_queue *q, unsigned int count) }return ret; } + (void )hws_read_active_state (hws, v->channel_index, &v->pix.interlaced);
[email protected]
maintainerPATCH v3 3/5 media: hws: add video bounce path for shared remap windows
In reply to [email protected]From: Ben Hoff <[email protected]>
The hardware selects BAR remap windows from the high bits of the device-side capture address. Since video and audio share one remap window per channel, direct programming of arbitrary vb2 DMA addresses is not reliable once audio capture also needs that window.
Add a per-channel video bounce path through the coherent scratch arena, copy completed frames into userspace buffers, and keep the threaded IRQ queue ownership rules explicit while pre-arming the next buffer.
--- drivers/media/pci/hws/hws.h | 3 + drivers/media/pci/hws/hws_irq.c | 272 +++++++++++++++++------- drivers/media/pci/hws/hws_irq.h | 1 + drivers/media/pci/hws/hws_pci.c | 16 +- drivers/media/pci/hws/hws_video.c | 330 +++++++++++++++++++++++++----- drivers/media/pci/hws/hws_video.h | 10 +- 6 files changed, 501 insertions(+), 131 deletions(-)
drivers/media/pci/hws/hws.h +3 -0
@@ -53,6 +53,7 @@ struct hwsvideo_buffer {int slot; }; +#define HWS_VIDEO_DIRECT_SLOT (-1)#define HWS_VIDEO_BOUNCE_SLOTS 2struct hws_video {
@@ -174,6 +175,8 @@ struct hws_pcie_dev {bool suspended;int irq; +spinlock_t irq_thread_lock;/* protects threaded video IRQ counters */ +unsigned int irq_pending_vdone[MAX_VID_CHANNELS ];/* Error flags */ int pci_lost;
drivers/media/pci/hws/hws_irq.c +194 -78
374 lines not on this page · raw message
drivers/media/pci/hws/hws_irq.h +1 -0
@@ -6,5 +6,6 @@#include "hws.h" irqreturn_t hws_irq_handler (int irq,void *info); +irqreturn_t hws_irq_thread (int irq,void *info);#endif /* HWS_INTERRUPT_H */
drivers/media/pci/hws/hws_pci.c +13 -3
@@ -28,6 +28,11 @@#define HWS_BUSY_POLL_DELAY_US 10#define HWS_BUSY_POLL_TIMEOUT_US 1000000 +static bool hws_enable_audio = true; +module_param_named (enable_audio, hws_enable_audio, bool,0644 ); +MODULE_PARM_DESC (enable_audio, +"Enable ALSA HDMI audio capture devices; set to 0 for video-only mode" ); +static unsigned long long hws_elapsed_us (u64 start_ns) {return div_u64 (ktime_get_mono_fast_ns () - start_ns,1000 );
@@ -125,6 +130,8 @@ static void hws_configure_hardware_capabilities(struct hws_pcie_dev *hdev)if (hdev->cur_max_audio_ch > hdev->cur_max_video_ch) hdev->cur_max_audio_ch = hdev->cur_max_video_ch; +if (!hws_enable_audio) + hdev->cur_max_audio_ch =0 ;/* universal buffer capacity */ hdev->max_hw_video_buf_sz =MAX_MM_VIDEO_SIZE ;
@@ -519,6 +526,7 @@ static int hws_probe(struct pci_dev *pdev, const struct pci_device_id *pci_id) hws->irq =-1 ; hws->suspended = false;mutex_init (&hws->scratch_lock); +spin_lock_init (&hws->irq_thread_lock);pci_set_drvdata (pdev, hws);/* 1) Enable device + bus mastering (managed) */
@@ -584,10 +592,12 @@ static int hws_probe(struct pci_dev *pdev, const struct pci_device_id *pci_id)hws_irq_clear_pending (hws);/* D) Request the legacy shared interrupt line (no vectors/MSI/MSI-X) */ - ret =devm_request_irq (&pdev->dev, irq, hws_irq_handler, irqf, -dev_name (&pdev->dev), hws); + ret =devm_request_threaded_irq (&pdev->dev, irq, hws_irq_handler, + hws_irq_thread, irqf,dev_name (&pdev->dev), + hws);if (ret) { -dev_err (&pdev->dev,"request_irq(%d) failed: %d\n" , irq, ret); +dev_err (&pdev->dev,"request_threaded_irq(%d) failed: %d\n" , + irq, ret); goto err_unwind_channels; }
drivers/media/pci/hws/hws_video.c +284 -46
501 lines not on this page · raw message
drivers/media/pci/hws/hws_video.h +6 -4
@@ -9,7 +9,7 @@ void hws_video_unregister(struct hws_pcie_dev *dev);void hws_enable_video_capture (struct hws_pcie_dev *hws,unsigned int chan,bool on); -void hws_prime_next_locked (struct hws_video *vid); +int hws_prime_next_locked (struct hws_video *vid);int hws_video_init_channel (struct hws_pcie_dev *pdev,int ch);void hws_video_cleanup_channel (struct hws_pcie_dev *pdev,int ch);
@@ -17,9 +17,11 @@ void check_video_format(struct hws_pcie_dev *pdx);int hws_check_card_status (struct hws_pcie_dev *hws);void hws_init_video_sys (struct hws_pcie_dev *hws,bool enable); -void hws_program_dma_for_addr (struct hws_pcie_dev *hws, -unsigned int ch, -dma_addr_t dma); +int hws_program_dma_for_buffer (struct hws_pcie_dev *hws, +unsigned int ch, +struct hwsvideo_buffer *buf); +int hws_video_prepare_done_buffer (struct hws_video *vid, +struct hwsvideo_buffer *buf);int hws_video_quiesce (struct hws_pcie_dev *hws,const char *reason);void hws_video_pm_resume (struct hws_pcie_dev *hws);
Signature · 1 line
2.54.0
[email protected]
maintainerPATCH v3 4/5 media: hws: harden video DMA queue ownership
In reply to [email protected]From: Ben Hoff <[email protected]>
Keep no-signal completion on the same queue ownership path as normal video completion. Program a newly selected buffer before publishing it as active, restore it to the capture queue if programming fails, and report the failure without leaving stale active or prepared-buffer state behind.
--- drivers/media/pci/hws/hws_video.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-)
drivers/media/pci/hws/hws_video.c +15 -10
@@ -330,8 +330,8 @@ static bool hws_force_no_signal_frame(struct hws_video *v, const char *tag)struct hws_pcie_dev *hws;unsigned long flags;struct hwsvideo_buffer *buf =NULL , *next =NULL ; -bool have_next = false;bool programmed = false; +int ret =0 ;if (!v)return false;
@@ -354,22 +354,31 @@ static bool hws_force_no_signal_frame(struct hws_video *v, const char *tag)if (v->next_prepared) { next = v->next_prepared; v->next_prepared =NULL ; - next->slot =HWS_VIDEO_DIRECT_SLOT ; v->active = next; - have_next = true; + programmed = true; }else if (!list_empty (&v->capture_queue)) { next =list_first_entry (&v->capture_queue, struct hwsvideo_buffer, list);list_del_init (&next->list);if (v->queued_count) v->queued_count--; - next->slot =HWS_VIDEO_DIRECT_SLOT ; - v->active = next; - have_next = true; + ret =hws_program_dma_for_buffer (hws, v->channel_index, next); +if (ret) { +list_add (&next->list, &v->capture_queue); + v->queued_count++; + next =NULL ; + }else { + v->active = next; + programmed = true; + } }else { v->active =NULL ; }spin_unlock_irqrestore (&v->irq_lock, flags); +if (ret) +dev_warn_ratelimited (&hws->pdev->dev, +"%s: failed to arm no-signal buffer ch=%u ret=%d\n" , + tag, v->channel_index, ret);if (!buf)return false;/* Complete buffer with a neutral frame so dequeuers keep running. */
@@ -385,10 +394,6 @@ static bool hws_force_no_signal_frame(struct hws_video *v, const char *tag) vb2v->vb2_buf.timestamp =ktime_get_ns ();vb2_buffer_done (&vb2v->vb2_buf,VB2_BUF_STATE_DONE ); } -if (have_next && next) { -if (!hws_program_dma_for_buffer (hws, v->channel_index, next)) - programmed = true; - }if (programmed) {wmb ();/* ensure descriptors visible before enabling capture */ hws_enable_video_capture (hws, v->channel_index, true);
Signature · 1 line
2.54.0
[email protected]
maintainerPATCH v3 5/5 media: hws: add HDMI audio capture support
In reply to [email protected]From: Ben Hoff <[email protected]>
Add ALSA PCM capture for the HDMI audio streams exposed by the HWS hardware. The audio path shares the per-channel scratch DMA arena and BAR remap slot with video, handles audio-done interrupts, exposes capture constraints through ALSA hw_params, and starts/stops the hardware capture engine from the PCM lifecycle.
--- drivers/media/pci/hws/Kconfig | 3 +- drivers/media/pci/hws/Makefile | 2 +- drivers/media/pci/hws/hws.h | 52 ++ drivers/media/pci/hws/hws_audio.c | 1183 +++++++++++++++++++++++++++++ drivers/media/pci/hws/hws_audio.h | 23 + drivers/media/pci/hws/hws_irq.c | 32 + drivers/media/pci/hws/hws_pci.c | 105 ++- drivers/media/pci/hws/hws_reg.h | 42 +- drivers/media/pci/hws/hws_video.c | 3 + 9 files changed, 1422 insertions(+), 23 deletions(-) create mode 100644 drivers/media/pci/hws/hws_audio.c create mode 100644 drivers/media/pci/hws/hws_audio.h
drivers/media/pci/hws/Kconfig +2 -1
@@ -1,8 +1,9 @@ # SPDX-License-Identifier: GPL-2.0-only config VIDEO_HWS tristate "AVMatrix HWS PCIe capture devices" - depends on PCI && VIDEO_DEV + depends on PCI && VIDEO_DEV && SND select VIDEOBUF2_DMA_CONTIG + select SND_PCM help Enable support for AVMatrix HWS series multi-channel PCIe capture devices that provide HDMI video capture.
drivers/media/pci/hws/Makefile +1 -1
@@ -1,4 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only -hws-objs := hws_pci.o hws_irq.o hws_video.o hws_v4l2_ioctl.o +hws-objs := hws_pci.o hws_irq.o hws_video.o hws_v4l2_ioctl.o hws_audio.o obj-$(CONFIG_VIDEO_HWS) += hws.o
drivers/media/pci/hws/hws.h +52 -0
94 lines not on this page · raw message
drivers/media/pci/hws/hws_audio.c new file +1183 -0
1,183 lines not on this page · raw message
drivers/media/pci/hws/hws_audio.h new file +23 -0
@@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef HWS_AUDIO_PIPELINE_H +#define HWS_AUDIO_PIPELINE_H + +#include <sound/pcm.h> +#include "hws.h" + +int hws_audio_register (struct hws_pcie_dev *dev); +void hws_audio_unregister (struct hws_pcie_dev *hws); +void hws_audio_seed_channels (struct hws_pcie_dev *hws); +void hws_audio_queue_interrupt (struct hws_pcie_dev *hws,unsigned int ch, +u8 cur_toggle); +void hws_audio_drain_work (struct hws_pcie_dev *hws); +void hws_enable_audio_capture (struct hws_pcie_dev *hws, +unsigned int ch, +bool enable); + +int hws_audio_init_channel (struct hws_pcie_dev *pdev,int ch); +void hws_audio_cleanup_channel (struct hws_pcie_dev *pdev,int ch,bool device_removal); +int hws_audio_pm_suspend_all (struct hws_pcie_dev *hws); +void hws_audio_pm_resume (struct hws_pcie_dev *hws); + +#endif /* HWS_AUDIO_PIPELINE_H */
drivers/media/pci/hws/hws_irq.c +32 -0
@@ -11,6 +11,7 @@#include "hws_reg.h" #include "hws_video.h" #include "hws.h" +#include "hws_audio.h" #define MAX_INT_LOOPS 100
@@ -314,6 +315,36 @@ static bool hws_irq_queue_video(struct hws_pcie_dev *pdx, u32 int_state)return wake_thread; } +static void hws_irq_handle_audio (struct hws_pcie_dev *pdx,u32 int_state) +{ +unsigned int ch; + +for (ch =0 ; ch < pdx->cur_max_audio_ch; ++ch) { +u32 abit =HWS_INT_ADONE_BIT (ch); +u8 cur_toggle; + +if (!(int_state & abit)) +continue ; + +/* Only service running streams */ +if (!READ_ONCE (pdx->audio[ch].cap_active) || + !READ_ONCE (pdx->audio[ch].stream_running) || +READ_ONCE (pdx->audio[ch].stop_requested)) +continue ; + +/* +* Baseline read ABUF_TOGGLE for every ADONE interrupt. +* The register reports the half the device is filling now, so +* the completed packet is the opposite half. Read it in the +* hard handler so the deferred audio work receives the edge's +* toggle value, not a later one. +*/ + cur_toggle =readl_relaxed (pdx->bar0_base + +HWS_REG_ABUF_TOGGLE (ch)) &0x01 ; +hws_audio_queue_interrupt (pdx, ch, cur_toggle); + } +} +irqreturn_t hws_irq_handler (int irq,void *info) {struct hws_pcie_dev *pdx = info;
@@ -351,6 +382,7 @@ irqreturn_t hws_irq_handler(int irq, void *info)dev_dbg (&pdx->pdev->dev,"irq: entry INT_STATUS=0x%08x\n" , int_state); wake_thread =hws_irq_queue_video (pdx, int_state); +hws_irq_handle_audio (pdx, int_state);hws_irq_ack_status (pdx, int_state);return wake_thread ?IRQ_WAKE_THREAD :IRQ_HANDLED ;
drivers/media/pci/hws/hws_pci.c +90 -15
237 lines not on this page · raw message
drivers/media/pci/hws/hws_reg.h +36 -6
77 lines not on this page · raw message
drivers/media/pci/hws/hws_video.c +3 -0
@@ -24,6 +24,7 @@#include "hws.h" #include "hws_reg.h" #include "hws_video.h" +#include "hws_audio.h" #include "hws_irq.h" #include "hws_v4l2_ioctl.h"
@@ -781,12 +782,14 @@ void hws_init_video_sys(struct hws_pcie_dev *hws, bool enable)/* 1) reset the decoder mode register to 0 */ writel (0x00000000 , hws->bar0_base +HWS_REG_DEC_MODE );hws_seed_dma_windows (hws); +hws_audio_seed_channels (hws);/* 3) on a full reset, clear all per-channel status and indices */ if (!enable) {for (i =0 ; i < hws->max_channels; i++) {/* helpers to arm/disable capture engines */ hws_enable_video_capture (hws, i, false); +hws_enable_audio_capture (hws, i, false); } }
Signature · 1 line
2.54.0