diff --git a/include/sound/sof/ipc4/header.h b/include/sound/sof/ipc4/header.h index 0f6f1de3eee90f..f21a1b6920bd1b 100644 --- a/include/sound/sof/ipc4/header.h +++ b/include/sound/sof/ipc4/header.h @@ -651,6 +651,8 @@ struct sof_ipc4_notify_resource_data { #define SOF_IPC4_DEBUG_SLOT_GDB_STUB 0x42444700 #define SOF_IPC4_DEBUG_SLOT_TELEMETRY 0x4c455400 #define SOF_IPC4_DEBUG_SLOT_BROKEN 0x44414544 +#define SOF_IPC4_DEBUG_SLOT_SHELL 0x73686c6c /* 'shll' Zephyr winstream shell */ +#define SOF_IPC4_DEBUG_SLOT_LLEXT_LOAD 0x4C454C44 /* 'LELD' shell llext_load rendezvous */ /** * struct sof_ipc4_notify_module_data - payload for module notification diff --git a/include/uapi/sound/sof/cdev.h b/include/uapi/sound/sof/cdev.h new file mode 100644 index 00000000000000..4228d81d56854f --- /dev/null +++ b/include/uapi/sound/sof/cdev.h @@ -0,0 +1,258 @@ +/* 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) 2026 Intel Corporation. All rights reserved. + * + * Sound Open Firmware (SOF) Character Device UAPI (/dev/sof-dspN) + */ + +#ifndef __INCLUDE_UAPI_SOUND_SOF_CDEV_H +#define __INCLUDE_UAPI_SOUND_SOF_CDEV_H + +#include +#include + +#define SOF_CDEV_IOC_MAGIC 'S' +#define SOF_CDEV_ABI_VERSION 0x00010000 + +/* + * SRAM Memory Window Selectors + * Used in struct sof_cdev_win_io.win_id and mmap() page offset (win_id << 20) + */ +#define SOF_CDEV_WIN_FW_REGS 0 /* Window 0: FW Regs (0x0..0xfff) + Inbox (+0x1000) */ +#define SOF_CDEV_WIN_OUTBOX 1 /* Window 1: Host Downlink Outbox */ +#define SOF_CDEV_WIN_DEBUG 2 /* Window 2: Debug Window (16 x 4 KB pages) */ +#define SOF_CDEV_WIN_PERF 3 /* Window 3: Performance Measurements Window (ADSP_PMW) */ +#define SOF_CDEV_WIN_BY_SLOT_TYPE 0xFF /* Select Window 2 slot by slot_type + slot_index */ + +#define SOF_CDEV_MAX_WINDOWS 4 +#define SOF_CDEV_MAX_DEBUG_SLOTS 16 + +/* Standard IPC4 Window 2 Debug Slot Types */ +#define SOF_CDEV_SLOT_UNUSED 0x00000000U +#define SOF_CDEV_SLOT_CRITICAL_LOG 0x54524300U +#define SOF_CDEV_SLOT_DEBUG_LOG 0x474f4c00U +#define SOF_CDEV_SLOT_GDB_STUB 0x42444700U +#define SOF_CDEV_SLOT_TELEMETRY 0x4c455400U +#define SOF_CDEV_SLOT_BROKEN 0x44414544U +#define SOF_CDEV_SLOT_SHELL 0x73686c6cU /* 'shll' Zephyr winstream shell */ +#define SOF_CDEV_SLOT_LLEXT_LOAD 0x4c454c44U /* 'LELD' Shell LLEXT load shm */ + +/** + * struct sof_cdev_slot_desc - Window 2 debug slot descriptor + * @slot_index: Slot index (0..14 for full 4 KB pages, 15 for partial page 0) + * @res_id: Resource / owner ID reported by firmware + * @type: Slot type magic (e.g. SOF_CDEV_SLOT_TELEMETRY) + * @vma: DSP virtual memory address of the slot + * @win_offset: Byte offset of the slot payload relative to Window 2 start + * @bar_offset: Byte offset of the slot payload relative to DSP BAR + * @size: Usable byte capacity of the slot (4096 for 0..14, 3072 for 15) + * @reserved: Reserved for alignment + */ +struct sof_cdev_slot_desc { + __u32 slot_index; + __u32 res_id; + __u32 type; + __u32 vma; + __u32 win_offset; + __u32 bar_offset; + __u32 size; + __u32 reserved; +}; + +/** + * struct sof_cdev_win_desc - SRAM Window descriptor + * @win_id: Window index (0..3) + * @bar_offset: Byte offset relative to DSP BAR (sdev->mailbox_bar) + * @size: Window byte size + * @flags: Access flags (bit 0 = valid/mapped) + */ +struct sof_cdev_win_desc { + __u32 win_id; + __u32 bar_offset; + __u32 size; + __u32 flags; +}; + +/** + * struct sof_cdev_info - Device, firmware, and window capability summary + */ +struct sof_cdev_info { + __u32 abi_version; + __u32 ipc_type; /* 0 = IPC3, 1 = IPC4 */ + __u32 fw_state; /* enum sof_fw_state */ + __u32 dsp_power_state; /* enum sof_dsp_power_states (0=U0, 1=D0, 2=D1, 3=D2, 4=D3) */ + __u32 pm_runtime_active; /* 1 if PCI/ACPI device is currently in D0 */ + __u32 num_cores; /* Total DSP cores on platform */ + __u32 enabled_cores_mask; /* Bitmask of currently enabled DSP cores */ + __u32 max_payload_size; /* Max IPC mailbox payload size in bytes */ + __u16 fw_ver_major; + __u16 fw_ver_minor; + __u16 fw_ver_micro; + __u16 fw_ver_build; + __u32 hw_ip_version; /* enum sof_intel_hw_ip_version */ + char platform[32]; /* Platform / driver identifier */ + char fw_filename[64]; /* Loaded firmware file path */ + struct sof_cdev_win_desc windows[SOF_CDEV_MAX_WINDOWS]; + struct sof_cdev_slot_desc debug_slots[SOF_CDEV_MAX_DEBUG_SLOTS]; +}; + +/* + * Power Management Control Commands (SOF_IOC_PM_CTL) + */ +#define SOF_PM_OP_QUERY 0 /* Query current power & core state without waking */ +#define SOF_PM_OP_WAKE_HOLD 1 /* Resume DSP to D0 and hold PM reference on this fd */ +#define SOF_PM_OP_RELEASE_HOLD 2 /* Release PM reference held on this fd */ +#define SOF_PM_OP_BOOT_DSP 3 /* Ensure DSP firmware is booted (sof_client_boot_dsp) */ +#define SOF_PM_OP_CORE_GET 4 /* Power up specific DSP core (core_id) */ +#define SOF_PM_OP_CORE_PUT 5 /* Power down specific DSP core (core_id) */ + +struct sof_cdev_pm_ctl { + __u32 op; /* SOF_PM_OP_* */ + __u32 core_id; /* Used for CORE_GET / CORE_PUT */ + __u32 fw_state; /* Out: current enum sof_fw_state */ + __u32 dsp_power_state; /* Out: current DSP power state */ + __u32 pm_runtime_active; /* Out: 1 if runtime PM is active (D0) */ + __u32 enabled_cores_mask; /* Out: current enabled_cores_mask */ + __u32 fd_holding_d0; /* Out: 1 if this fd currently holds a D0 reference */ + __u32 reserved; +}; + +/* + * Atomic IPC Transaction (SOF_IOC_IPC_TX) + */ +#define SOF_CDEV_IPC_FL_NO_REPLY (1U << 0) /* Do not expect payload reply */ + +struct sof_cdev_ipc_msg { + __u32 primary; /* In: IPC4 primary header (or IPC3 cmd) */ + __u32 extension; /* In: IPC4 extension header */ + __u32 tx_size; /* In: TX payload byte length */ + __u32 rx_size; /* In: RX buffer capacity; Out: bytes received */ + __u32 reply_primary; /* Out: IPC4 reply primary header */ + __u32 reply_extension; /* Out: IPC4 reply extension header */ + __s32 ipc_status; /* Out: Firmware reply status (0 = success) */ + __u32 flags; /* In: SOF_CDEV_IPC_FL_* */ + __u64 tx_data_ptr; /* In: Userspace pointer to TX payload (or 0) */ + __u64 rx_data_ptr; /* In: Userspace pointer to RX buffer (or 0) */ +}; + +/* + * IPC4 Large Config Get / Set (SOF_IOC_IPC_LARGE_CFG) + */ +#define SOF_CDEV_LARGE_CFG_GET 0 +#define SOF_CDEV_LARGE_CFG_SET 1 + +struct sof_cdev_large_cfg { + __u32 module_id; /* 0 for BaseFW, or target module_id */ + __u32 instance_id; /* 0 for BaseFW, or target instance_id */ + __u32 param_id; /* IPC4 param ID (e.g. 14=MEMORY_STATE, 15=POWER_STATE) */ + __u32 set_get; /* SOF_CDEV_LARGE_CFG_GET (0) or SET (1) */ + __u32 data_size; /* In: buffer size; Out: actual bytes read/written */ + __u32 flags; + __u64 data_ptr; /* Userspace buffer pointer */ +}; + +/* + * Memory Window & Debug Slot I/O (SOF_IOC_WIN_INFO, SOF_IOC_WIN_READ, SOF_IOC_WIN_WRITE) + */ +#define SOF_CDEV_WIN_FL_WAKE_DSP (1U << 0) /* Wake DSP to D0 if currently suspended */ + +struct sof_cdev_win_info { + struct sof_cdev_win_desc windows[SOF_CDEV_MAX_WINDOWS]; + struct sof_cdev_slot_desc debug_slots[SOF_CDEV_MAX_DEBUG_SLOTS]; +}; + +struct sof_cdev_win_io { + __u32 win_id; /* SOF_CDEV_WIN_0..3 or SOF_CDEV_WIN_BY_SLOT_TYPE */ + __u32 slot_type; /* Used when win_id == SOF_CDEV_WIN_BY_SLOT_TYPE */ + __u32 slot_index; /* Match Nth slot of slot_type (0 = first match) */ + __u32 offset; /* Byte offset within window or slot */ + __u32 size; /* Bytes to read or write */ + __u32 flags; /* SOF_CDEV_WIN_FL_* */ + __u64 data_ptr; /* Userspace buffer pointer */ +}; + +/* + * Zephyr Winstream Slot I/O (SOF_IOC_WINSTREAM_IO) + * Used for ADSP_DW_SLOT_SHELL (0x73686c6c) and GDB stub (0x42444700) + */ +struct sof_cdev_winstream_io { + __u32 slot_type; /* Default: SOF_CDEV_SLOT_SHELL (0x73686c6c) */ + __u32 rx_win_size; /* Host->DSP winstream region size (default 256) */ + __u32 tx_len; /* Bytes to write from tx_ptr to DSP (0 = read only) */ + __u32 rx_buf_len; /* Max bytes to read into rx_ptr (0 = write only) */ + __u32 rx_bytes_read; /* Out: Actual bytes read from DSP winstream */ + __u32 seq_in_out; /* In/Out: Host reader sequence number */ + __u64 tx_ptr; /* Userspace pointer to TX data */ + __u64 rx_ptr; /* Userspace pointer to RX buffer */ +}; + +/* + * Atomic LLEXT Module Loading (SOF_IOC_LLEXT_LOAD) + */ +#define SOF_LLEXT_MODE_SHELL_SHM 0 /* 2-step handshake via SOF_CDEV_SLOT_LLEXT_LOAD */ +#define SOF_LLEXT_MODE_IPC4_DIRECT 1 /* Direct IPC4 LOAD_LIBRARY with explicit/auto lib_id */ + +struct sof_cdev_llext_load { + __u32 mode; /* SOF_LLEXT_MODE_* */ + __u32 lib_id; /* In/Out: Library slot ID (1..15; 0 = from shm or auto) */ + __u32 data_size; /* Size of .ri / .llext binary in bytes */ + __s32 fw_state; /* Out: Final slot state or IPC status */ + __u64 data_ptr; /* Userspace pointer to library binary */ +}; + +/* + * SOF Probes Control (SOF_IOC_PROBE_CTL) + */ +#define SOF_PROBE_OP_INIT 1 +#define SOF_PROBE_OP_DEINIT 2 +#define SOF_PROBE_OP_INFO_ACTIVE 3 +#define SOF_PROBE_OP_INFO_AVAILABLE 4 +#define SOF_PROBE_OP_POINTS_ADD 5 +#define SOF_PROBE_OP_POINTS_REMOVE 6 + +struct sof_cdev_probe_point { + __u32 buffer_id; /* Probe point ID (mod_id, inst_id, type, idx) */ + __u32 purpose; /* 0 = EXTRACTION, 1 = INJECTION */ + __u32 stream_tag; /* HDA stream tag (or 0 if disconnected) */ + __u32 reserved; + char widget_name[32]; /* Resolved topology widget name (output on INFO) */ +}; + +struct sof_cdev_probe_ctl { + __u32 op; /* SOF_PROBE_OP_* */ + __u32 stream_tag; /* Used for SOF_PROBE_OP_INIT */ + __u32 dma_buffer_size; /* Used for SOF_PROBE_OP_INIT */ + __u32 num_points; /* In: capacity / count; Out: returned count */ + __u64 points_ptr; /* Userspace pointer to struct sof_cdev_probe_point[] */ +}; + +/* + * Async Event Notification Record read() from /dev/sof-dspN + */ +#define SOF_CDEV_EVT_FW_STATE 1 +#define SOF_CDEV_EVT_IPC_NOTIFICATION 2 + +struct sof_cdev_event { + __u64 timestamp_ns; /* ktime_get_ns() */ + __u32 event_type; /* SOF_CDEV_EVT_* */ + __u32 fw_state; /* Current enum sof_fw_state */ + __u32 primary; /* IPC4 notification primary header */ + __u32 extension; /* IPC4 notification extension header */ +}; + +/* IOCTL Definitions */ +#define SOF_IOC_GET_INFO _IOR(SOF_CDEV_IOC_MAGIC, 0x01, struct sof_cdev_info) +#define SOF_IOC_PM_CTL _IOWR(SOF_CDEV_IOC_MAGIC, 0x02, struct sof_cdev_pm_ctl) +#define SOF_IOC_IPC_TX _IOWR(SOF_CDEV_IOC_MAGIC, 0x10, struct sof_cdev_ipc_msg) +#define SOF_IOC_IPC_LARGE_CFG _IOWR(SOF_CDEV_IOC_MAGIC, 0x11, struct sof_cdev_large_cfg) +#define SOF_IOC_WIN_INFO _IOR(SOF_CDEV_IOC_MAGIC, 0x20, struct sof_cdev_win_info) +#define SOF_IOC_WIN_READ _IOWR(SOF_CDEV_IOC_MAGIC, 0x21, struct sof_cdev_win_io) +#define SOF_IOC_WIN_WRITE _IOW(SOF_CDEV_IOC_MAGIC, 0x22, struct sof_cdev_win_io) +#define SOF_IOC_WINSTREAM_IO _IOWR(SOF_CDEV_IOC_MAGIC, 0x23, struct sof_cdev_winstream_io) +#define SOF_IOC_LLEXT_LOAD _IOWR(SOF_CDEV_IOC_MAGIC, 0x30, struct sof_cdev_llext_load) +#define SOF_IOC_PROBE_CTL _IOWR(SOF_CDEV_IOC_MAGIC, 0x40, struct sof_cdev_probe_ctl) + +#endif /* __INCLUDE_UAPI_SOUND_SOF_CDEV_H */ diff --git a/sound/soc/sof/Kconfig b/sound/soc/sof/Kconfig index f73a0e534851c8..9f7a78db846ead 100644 --- a/sound/soc/sof/Kconfig +++ b/sound/soc/sof/Kconfig @@ -271,6 +271,16 @@ config SND_SOC_SOF_DEBUG_FW_GDB socket, to which GDB can then connect. If unsure, select "N". +config SND_SOC_SOF_CLIENT_CDEV + tristate "SOF character device client (/dev/sof-dspN)" + depends on SND_SOC_SOF && SND_SOC_SOF_IPC4 + select SND_SOC_SOF_CLIENT + help + This enables the SOF character device client driver (/dev/sof-dspN) + exposing ioctls for atomic IPC transactions, SRAM memory window I/O + and mmap, LLEXT module loading, Zephyr winstream shell/GDB access, + and SOF probe point control. + config SND_SOC_SOF_DEBUG_RETAIN_DSP_CONTEXT bool "SOF retain DSP context on any FW exceptions" help diff --git a/sound/soc/sof/Makefile b/sound/soc/sof/Makefile index 887abf70da3ba1..209514bc67cc9c 100644 --- a/sound/soc/sof/Makefile +++ b/sound/soc/sof/Makefile @@ -36,6 +36,7 @@ ifneq ($(CONFIG_SND_SOC_SOF_IPC4),) snd-sof-probes-y += sof-client-probes-ipc4.o endif snd-sof-fw-gdb-objs := sof-client-fw-gdb.o +snd-sof-client-cdev-y := sof-client-cdev.o snd-sof-nocodec-y := nocodec.o @@ -55,6 +56,7 @@ obj-$(CONFIG_SND_SOC_SOF_DEBUG_IPC_MSG_INJECTOR) += snd-sof-ipc-msg-injector.o obj-$(CONFIG_SND_SOC_SOF_DEBUG_IPC_KERNEL_INJECTOR) += snd-sof-ipc-kernel-injector.o obj-$(CONFIG_SND_SOC_SOF_DEBUG_PROBES) += snd-sof-probes.o obj-$(CONFIG_SND_SOC_SOF_DEBUG_FW_GDB) += snd-sof-fw-gdb.o +obj-$(CONFIG_SND_SOC_SOF_CLIENT_CDEV) += snd-sof-client-cdev.o obj-$(CONFIG_SND_SOC_SOF_INTEL_TOPLEVEL) += intel/ obj-$(CONFIG_SND_SOC_SOF_IMX_TOPLEVEL) += imx/ diff --git a/sound/soc/sof/ipc4-loader.c b/sound/soc/sof/ipc4-loader.c index 1da51f7490e579..2e492711da379e 100644 --- a/sound/soc/sof/ipc4-loader.c +++ b/sound/soc/sof/ipc4-loader.c @@ -252,6 +252,94 @@ static int sof_ipc4_load_library(struct snd_sof_dev *sdev, unsigned long lib_id, return ret; } +/** + * snd_sof_ipc4_load_library_from_buf - load an IPC4 library from an in-memory buffer + * @sdev: SOF device + * @lib_id: target library slot [1 .. max_libs_count - 1] + * @buf: raw library binary (rimage format with ext-manifest header) + * @size: size of the binary in bytes + * + * Return: 0 on success, negative errno on error. + */ +int snd_sof_ipc4_load_library_from_buf(struct snd_sof_dev *sdev, u32 lib_id, + const void *buf, size_t size) +{ + struct sof_ipc4_fw_data *ipc4_data = sdev->private; + struct sof_ipc4_fw_library *old_lib; + struct sof_ipc4_fw_library *fw_lib; + struct firmware *fake_fw; + ssize_t payload_offset; + int i, ret; + + if (!ipc4_data || !ipc4_data->load_library) { + dev_err(sdev->dev, "Library loading not supported on this platform\n"); + return -EOPNOTSUPP; + } + + if (!lib_id || lib_id >= ipc4_data->max_libs_count) { + dev_err(sdev->dev, "Invalid lib_id %u (max %u)\n", + lib_id, ipc4_data->max_libs_count - 1); + return -EINVAL; + } + + fake_fw = devm_kzalloc(sdev->dev, sizeof(*fake_fw), GFP_KERNEL); + if (!fake_fw) + return -ENOMEM; + fake_fw->data = buf; + fake_fw->size = size; + + fw_lib = devm_kzalloc(sdev->dev, sizeof(*fw_lib), GFP_KERNEL); + if (!fw_lib) { + devm_kfree(sdev->dev, fake_fw); + return -ENOMEM; + } + + fw_lib->sof_fw.fw = fake_fw; + fw_lib->name = "cdev-llext"; + + payload_offset = sof_ipc4_fw_parse_ext_man(sdev, fw_lib); + if (payload_offset <= 0) { + ret = payload_offset ? (int)payload_offset : -EINVAL; + goto free_fw_lib; + } + + fw_lib->sof_fw.payload_offset = payload_offset; + fw_lib->id = lib_id; + + for (i = 0; i < fw_lib->num_modules; i++) + fw_lib->modules[i].man4_module_entry.id |= + (lib_id << SOF_IPC4_MOD_LIB_ID_SHIFT); + + ret = ipc4_data->load_library(sdev, fw_lib, false); + if (ret) + goto free_modules; + + /* Allow replacing a previously purged interactive library in the same slot */ + old_lib = xa_erase(&ipc4_data->fw_lib_xa, lib_id); + if (old_lib) { + devm_kfree(sdev->dev, old_lib->modules); + devm_kfree(sdev->dev, old_lib); + } + + ret = xa_insert(&ipc4_data->fw_lib_xa, lib_id, fw_lib, GFP_KERNEL); + if (unlikely(ret)) { + dev_err(sdev->dev, "Library ID %u already registered\n", lib_id); + goto free_modules; + } + + dev_info(sdev->dev, "LLEXT library loaded as lib_id=%u (%zu bytes)\n", + lib_id, size); + return 0; + +free_modules: + devm_kfree(sdev->dev, fw_lib->modules); +free_fw_lib: + devm_kfree(sdev->dev, fw_lib); + devm_kfree(sdev->dev, fake_fw); + return ret; +} +EXPORT_SYMBOL_NS_GPL(snd_sof_ipc4_load_library_from_buf, "SND_SOC_SOF"); + /** * sof_ipc4_complete_split_release - loads the library parts of a split firmware * @sdev: SOF device diff --git a/sound/soc/sof/ipc4-priv.h b/sound/soc/sof/ipc4-priv.h index 31f7d68029ce71..374643c53eb42b 100644 --- a/sound/soc/sof/ipc4-priv.h +++ b/sound/soc/sof/ipc4-priv.h @@ -172,6 +172,8 @@ int sof_ipc4_get_stream_start_offset(struct snd_sof_dev *sdev, struct snd_sof_pcm_stream *sps, struct sof_ipc4_timestamp_info *time_info); u64 sof_ipc4_frames_dai_to_host(struct sof_ipc4_timestamp_info *time_info, u64 value); +int snd_sof_ipc4_load_library_from_buf(struct snd_sof_dev *sdev, u32 lib_id, + const void *buf, size_t size); #if IS_ENABLED(CONFIG_SND_SOC_SOF_COMPRESS) void sof_ipc4_compr_drain_done(struct snd_sof_dev *sdev, void *ipc_message); diff --git a/sound/soc/sof/sof-client-cdev.c b/sound/soc/sof/sof-client-cdev.c new file mode 100644 index 00000000000000..40e6fe40f5b94a --- /dev/null +++ b/sound/soc/sof/sof-client-cdev.c @@ -0,0 +1,1662 @@ +// SPDX-License-Identifier: GPL-2.0-only +// +// Copyright(c) 2026 Intel Corporation. All rights reserved. +// +// SOF auxiliary client character driver (/dev/sof-dspN) exposing DSP +// firmware debug, telemetry, IPC, memory window, LLEXT module loading, +// winstream shell/GDB, and probe operations to host userspace. + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ipc4-priv.h" +#include "ops.h" +#include "sof-audio.h" +#include "sof-client.h" +#include "sof-priv.h" + +#define SOF_CDEV_SUSPEND_DELAY_MS 3000 +#define SOF_CDEV_EVENT_FIFO_LEN 64 +#define SOF_CDEV_MAX_LARGE_CFG_BYTES (256 * 1024) +#define SOF_CDEV_MAX_LLEXT_BYTES (8 * 1024 * 1024) + +/* Zephyr winstream header layout */ +#define SOF_WINSTREAM_HDR_SIZE 16 +#define SOF_WINSTREAM_DATA_MAX(wsz) ((wsz) - SOF_WINSTREAM_HDR_SIZE) + +struct sof_winstream_hdr { + __le32 len; + __le32 start; + __le32 end; + __le32 seq; + u8 data[]; +} __packed; + +/* Shared mailbox layout for DSP shell "sof llext_load" command */ +#define SOF_SHELL_LLEXT_MAGIC 0x4C454C44U /* 'LELD' */ + +enum sof_shell_llext_state { + SOF_SHELL_LLEXT_IDLE = 0, + SOF_SHELL_LLEXT_REQUESTING = 1, + SOF_SHELL_LLEXT_DMA_ACTIVE = 2, + SOF_SHELL_LLEXT_DMA_DONE = 3, + SOF_SHELL_LLEXT_ERROR = 4, +}; + +struct sof_shell_llext_slot { + __u32 magic; + __u32 state; + __u32 lib_id; + __u32 xfer_bytes; + __s32 result; + __u32 reserved[3]; + char name[64]; +} __packed; + +/* IPC4 Probe constants matching sof-client-probes-ipc4.c */ +enum sof_ipc4_dma_type { + SOF_IPC4_DMA_HDA_HOST_OUTPUT = 0, + SOF_IPC4_DMA_HDA_HOST_INPUT = 1, +}; + +enum sof_ipc4_probe_runtime_param { + SOF_IPC4_PROBE_INJECTION_DMA = 1, + SOF_IPC4_PROBE_INJECTION_DMA_DETACH, + SOF_IPC4_PROBE_POINTS, + SOF_IPC4_PROBE_POINTS_DISCONNECT, + SOF_IPC4_PROBE_POINTS_AVAILABLE, +}; + +struct sof_ipc4_probe_gtw_cfg { + u32 node_id; + u32 dma_buffer_size; +} __packed __aligned(4); + +#define SOF_IPC4_PROBE_NODE_ID_INDEX(x) ((x) & GENMASK(7, 0)) +#define SOF_IPC4_PROBE_NODE_ID_TYPE(x) (((x) << 8) & GENMASK(12, 8)) + +struct sof_ipc4_probe_cfg { + struct sof_ipc4_base_module_cfg base; + struct sof_ipc4_probe_gtw_cfg gtw_cfg; +} __packed __aligned(4); + +struct sof_ipc4_probe_point { + u32 point_id; + u32 purpose; + u32 stream_tag; +} __packed __aligned(4); + +struct sof_ipc4_probe_info { + unsigned int num_elems; + DECLARE_FLEX_ARRAY(struct sof_ipc4_probe_point, points); +} __packed; + +#define SOF_PROBE_INVALID_PIPELINE_ID 0xFF + +struct sof_cdev_priv { + struct sof_client_dev *cdev; + struct miscdevice miscdev; + char dev_name[24]; + struct mutex io_lock; + struct mutex fh_lock; + struct list_head fh_list; + struct sof_man4_module *probe_mentry; +}; + +struct sof_cdev_fh { + struct sof_cdev_priv *priv; + struct list_head list; + wait_queue_head_t wait; + spinlock_t fifo_lock; + DECLARE_KFIFO(events, struct sof_cdev_event, SOF_CDEV_EVENT_FIFO_LEN); + bool holding_d0; +}; + +/* ------------------------------------------------------------------------- + * DSP Power State (D3 / D0i3 -> D0i0) & Window Helpers + * ------------------------------------------------------------------------- + */ + +static bool sof_cdev_is_dsp_d0i0(struct snd_sof_dev *sdev) +{ + return pm_runtime_active(sdev->dev) && + sdev->fw_state == SOF_FW_BOOT_COMPLETE && + sdev->dsp_power_state.state == SOF_DSP_PM_D0 && + sdev->dsp_power_state.substate == 0; +} + +static int sof_cdev_wake_dsp(struct sof_client_dev *cdev) +{ + struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev); + struct device *dev = &cdev->auxdev.dev; + const struct sof_dsp_power_state target_d0i0 = { + .state = SOF_DSP_PM_D0, + .substate = 0, + }; + int ret; + + ret = pm_runtime_resume_and_get(dev); + if (ret < 0 && ret != -EACCES) + return ret; + + ret = sof_client_boot_dsp(cdev); + if (ret < 0) { + pm_runtime_put_autosuspend(dev); + return ret; + } + + ret = snd_sof_dsp_set_power_state(sdev, &target_d0i0); + if (ret < 0) { + pm_runtime_put_autosuspend(dev); + return ret; + } + + return 0; +} + +static void sof_cdev_idle_dsp(struct sof_client_dev *cdev) +{ + struct device *dev = &cdev->auxdev.dev; + + pm_runtime_mark_last_busy(dev); + pm_runtime_put_autosuspend(dev); +} + +static u32 sof_cdev_get_window_size(struct snd_sof_dev *sdev, u32 win_id) +{ + switch (win_id) { + case SOF_CDEV_WIN_FW_REGS: + case SOF_CDEV_WIN_OUTBOX: + case SOF_CDEV_WIN_PERF: + return 0x2000; /* 8 KB mapped in DMWLO0/1/3 */ + case SOF_CDEV_WIN_DEBUG: + return 0x3000; /* 12 KB mapped in DMWLO2 on ACE/cAVS */ + default: + return 0x2000; + } +} + +static void sof_cdev_fill_windows_and_slots(struct snd_sof_dev *sdev, + struct sof_cdev_win_desc *wins, + struct sof_cdev_slot_desc *slots) +{ + u32 win2_size = sof_cdev_get_window_size(sdev, SOF_CDEV_WIN_DEBUG); + int pm_ref; + int i; + + memset(wins, 0, sizeof(*wins) * SOF_CDEV_MAX_WINDOWS); + memset(slots, 0, sizeof(*slots) * SOF_CDEV_MAX_DEBUG_SLOTS); + + for (i = 0; i < SOF_CDEV_MAX_WINDOWS; i++) { + int off = snd_sof_dsp_get_window_offset(sdev, i); + + wins[i].win_id = i; + if (off >= 0 && sdev->bar[sdev->mailbox_bar]) { + wins[i].bar_offset = (u32)off; + wins[i].size = sof_cdev_get_window_size(sdev, i); + wins[i].flags = 1; + } + } + + if (!wins[SOF_CDEV_WIN_DEBUG].flags) + return; + + pm_ref = pm_runtime_get_if_active(sdev->dev); + if (pm_ref <= 0) + return; + + if (!sof_cdev_is_dsp_d0i0(sdev)) { + pm_runtime_put_noidle(sdev->dev); + return; + } + + for (i = 0; i < SOF_CDEV_MAX_DEBUG_SLOTS; i++) { + u32 desc_off = sdev->debug_box.offset + + i * SOF_IPC4_DEBUG_DESCRIPTOR_SIZE; + u32 raw[3] = { 0 }; + u32 win_offset, slot_sz; + + if (i == SOF_CDEV_MAX_DEBUG_SLOTS - 1) { + win_offset = SOF_IPC4_DEBUG_PAGE0_SLOT_OFFSET; + slot_sz = SOF_IPC4_DEBUG_SLOT_SIZE - + SOF_IPC4_DEBUG_PAGE0_SLOT_OFFSET; + } else { + win_offset = (i + 1) * SOF_IPC4_DEBUG_SLOT_SIZE; + slot_sz = SOF_IPC4_DEBUG_SLOT_SIZE; + } + + sof_mailbox_read(sdev, desc_off, raw, sizeof(raw)); + + slots[i].slot_index = i; + if (raw[1] != 0 && raw[1] != 0xffffffffU && + win_offset + slot_sz <= win2_size) { + slots[i].res_id = raw[0]; + slots[i].type = raw[1]; + slots[i].vma = raw[2]; + } + slots[i].win_offset = win_offset; + slots[i].size = slot_sz; + slots[i].bar_offset = sdev->debug_box.offset + win_offset; + } + + pm_runtime_put_noidle(sdev->dev); +} + +static int sof_cdev_resolve_slot(struct snd_sof_dev *sdev, u32 slot_type, + u32 match_idx, u32 *bar_offset, u32 *slot_size) +{ + u32 win2_size = sof_cdev_get_window_size(sdev, SOF_CDEV_WIN_DEBUG); + u32 found = 0; + int i; + + if (!slot_type || slot_type == 0xffffffffU) + return -EINVAL; + + for (i = 0; i < SOF_CDEV_MAX_DEBUG_SLOTS; i++) { + u32 desc_type_off = sdev->debug_box.offset + + i * SOF_IPC4_DEBUG_DESCRIPTOR_SIZE + + sizeof(u32); + u32 type = 0; + u32 win_off, sz; + + if (i == SOF_CDEV_MAX_DEBUG_SLOTS - 1) { + win_off = SOF_IPC4_DEBUG_PAGE0_SLOT_OFFSET; + sz = SOF_IPC4_DEBUG_SLOT_SIZE - + SOF_IPC4_DEBUG_PAGE0_SLOT_OFFSET; + } else { + win_off = (i + 1) * SOF_IPC4_DEBUG_SLOT_SIZE; + sz = SOF_IPC4_DEBUG_SLOT_SIZE; + } + + if (win_off + sz > win2_size) + continue; + + sof_mailbox_read(sdev, desc_type_off, &type, sizeof(type)); + if (!type || type == 0xffffffffU) + continue; + + if (type == slot_type || + ((slot_type == SOF_CDEV_SLOT_CRITICAL_LOG || + slot_type == SOF_CDEV_SLOT_DEBUG_LOG) && + (type & 0xffffff00U) == slot_type)) { + if (found == match_idx) { + *bar_offset = sdev->debug_box.offset + win_off; + *slot_size = sz; + return 0; + } + found++; + } + } + + return -ENOENT; +} + +/* ------------------------------------------------------------------------- + * Zephyr Winstream Helpers + * ------------------------------------------------------------------------- + */ + +static u32 sof_ws_idx_mod(u32 idx, u32 len) +{ + return idx >= len ? idx - len : idx; +} + +static u32 sof_ws_idx_sub(u32 a, u32 b, u32 len) +{ + return sof_ws_idx_mod(a + (len - b), len); +} + +static void sof_ws_write(struct sof_winstream_hdr *ws, size_t win_size, + const u8 *data, u32 len0) +{ + u32 expected = SOF_WINSTREAM_DATA_MAX(win_size); + u32 ws_len = le32_to_cpu(ws->len); + u32 start, end, seq, len, suffix; + + if (ws_len != expected) { + ws->len = cpu_to_le32(expected); + ws->start = 0; + ws->end = 0; + ws->seq = 0; + ws_len = expected; + } + + start = le32_to_cpu(ws->start); + end = le32_to_cpu(ws->end); + seq = le32_to_cpu(ws->seq); + len = len0; + + if (!ws_len) + return; + + if (len > ws_len - 1) { + start = end; + len = ws_len - 1; + } + + len = min(len, ws_len); + if (seq != 0) { + u32 avail = (ws_len - 1) - sof_ws_idx_sub(end, start, ws_len); + + if (len > avail) + start = sof_ws_idx_mod(start + (len - avail), ws_len); + } + + if (len < len0) { + start = end; + data += len0 - len; + } + + suffix = min(len, ws_len - end); + memcpy(&ws->data[end], data, suffix); + if (len > suffix) + memcpy(&ws->data[0], data + suffix, len - suffix); + + end = sof_ws_idx_mod(end + len, ws_len); + + ws->start = cpu_to_le32(start); + ws->end = cpu_to_le32(end); + ws->seq = cpu_to_le32(seq + len0); +} + +static u32 sof_ws_read(struct sof_winstream_hdr *ws, size_t win_size, + u32 *seq, u8 *buf, u32 buflen) +{ + u32 expected = SOF_WINSTREAM_DATA_MAX(win_size); + u32 ws_len = le32_to_cpu(ws->len); + u32 ws_start = le32_to_cpu(ws->start); + u32 ws_end = le32_to_cpu(ws->end); + u32 ws_seq = le32_to_cpu(ws->seq); + u32 seq0 = *seq; + u32 len, behind, copy, suffix; + + if (ws_len != expected || !ws_len || !buflen) + return 0; + + if (*seq == 0) { + /* First read: start from current available window contents */ + u32 buffered = sof_ws_idx_sub(ws_end, ws_start, ws_len); + + *seq = (ws_seq >= buffered) ? (ws_seq - buffered) : ws_seq; + seq0 = *seq; + } + + if (*seq == ws_seq || ws_start == ws_end) { + *seq = ws_seq; + return 0; + } + + behind = ws_seq - *seq; + if (behind > sof_ws_idx_sub(ws_end, ws_start, ws_len)) { + u32 buffered = sof_ws_idx_sub(ws_end, ws_start, ws_len); + + *seq = ws_seq - buffered; + seq0 = *seq; + behind = buffered; + } + + copy = sof_ws_idx_sub(ws_end, behind, ws_len); + len = min(buflen, behind); + suffix = min(len, ws_len - copy); + + memcpy(buf, &ws->data[copy], suffix); + if (len > suffix) + memcpy(buf + suffix, &ws->data[0], len - suffix); + + *seq = seq0 + len; + return len; +} + +/* ------------------------------------------------------------------------- + * IOCTL Handlers + * ------------------------------------------------------------------------- + */ + +static int sof_cdev_ioc_get_info(struct sof_cdev_priv *priv, void __user *argp) +{ + struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(priv->cdev); + const struct sof_ipc_fw_version *ver = sof_client_get_fw_version(priv->cdev); + struct sof_cdev_info *info; + int ret = 0; + + info = kzalloc_obj(*info); + if (!info) + return -ENOMEM; + + info->abi_version = SOF_CDEV_ABI_VERSION; + info->ipc_type = sof_client_get_ipc_type(priv->cdev); + info->fw_state = sof_client_get_fw_state(priv->cdev); + info->dsp_power_state = sdev->dsp_power_state.state; + info->pm_runtime_active = pm_runtime_active(sdev->dev) ? 1 : 0; + info->num_cores = sdev->num_cores ? sdev->num_cores : 1; + info->enabled_cores_mask = sdev->enabled_cores_mask; + info->max_payload_size = sof_client_get_ipc_max_payload_size(priv->cdev); + + if (sdev->fw_version.major || sdev->fw_version.minor) { + info->fw_ver_major = sdev->fw_version.major; + info->fw_ver_minor = sdev->fw_version.minor; + info->fw_ver_micro = sdev->fw_version.micro; + info->fw_ver_build = sdev->fw_version.build; + } else if (ver) { + info->fw_ver_major = ver->major; + info->fw_ver_minor = ver->minor; + info->fw_ver_micro = ver->micro; + info->fw_ver_build = ver->build; + } + + if (sdev->pdata) { + if (sdev->pdata->name) + strscpy(info->platform, sdev->pdata->name, + sizeof(info->platform)); + else if (sdev->pdata->platform) + strscpy(info->platform, sdev->pdata->platform, + sizeof(info->platform)); + + if (sdev->pdata->fw_filename) + scnprintf(info->fw_filename, sizeof(info->fw_filename), + "%s/%s", + sdev->pdata->fw_filename_prefix ? + sdev->pdata->fw_filename_prefix : "", + sdev->pdata->fw_filename); + } + + mutex_lock(&priv->io_lock); + sof_cdev_fill_windows_and_slots(sdev, info->windows, info->debug_slots); + mutex_unlock(&priv->io_lock); + + if (copy_to_user(argp, info, sizeof(*info))) + ret = -EFAULT; + + kfree(info); + return ret; +} + +static int sof_cdev_ioc_pm_ctl(struct sof_cdev_fh *fh, void __user *argp) +{ + struct sof_cdev_priv *priv = fh->priv; + struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(priv->cdev); + struct sof_cdev_pm_ctl ctl; + int ret = 0; + + if (copy_from_user(&ctl, argp, sizeof(ctl))) + return -EFAULT; + + mutex_lock(&priv->io_lock); + + switch (ctl.op) { + case SOF_PM_OP_QUERY: + break; + case SOF_PM_OP_WAKE_HOLD: + if (!fh->holding_d0) { + ret = sof_client_core_module_get(priv->cdev); + if (ret < 0) + break; + ret = sof_cdev_wake_dsp(priv->cdev); + if (ret < 0) { + sof_client_core_module_put(priv->cdev); + break; + } + fh->holding_d0 = true; + } + break; + case SOF_PM_OP_RELEASE_HOLD: + if (fh->holding_d0) { + sof_cdev_idle_dsp(priv->cdev); + sof_client_core_module_put(priv->cdev); + fh->holding_d0 = false; + } + break; + case SOF_PM_OP_BOOT_DSP: + ret = sof_cdev_wake_dsp(priv->cdev); + if (ret >= 0) + sof_cdev_idle_dsp(priv->cdev); + break; + case SOF_PM_OP_CORE_GET: + if (ctl.core_id >= sdev->num_cores) { + ret = -EINVAL; + break; + } + ret = snd_sof_dsp_core_get(sdev, ctl.core_id); + break; + case SOF_PM_OP_CORE_PUT: + if (ctl.core_id >= sdev->num_cores) { + ret = -EINVAL; + break; + } + ret = snd_sof_dsp_core_put(sdev, ctl.core_id); + break; + default: + ret = -EINVAL; + break; + } + + ctl.fw_state = sof_client_get_fw_state(priv->cdev); + ctl.dsp_power_state = sdev->dsp_power_state.state; + ctl.pm_runtime_active = pm_runtime_active(sdev->dev) ? 1 : 0; + ctl.enabled_cores_mask = sdev->enabled_cores_mask; + ctl.fd_holding_d0 = fh->holding_d0 ? 1 : 0; + + mutex_unlock(&priv->io_lock); + + if (ret < 0) + return ret; + + if (copy_to_user(argp, &ctl, sizeof(ctl))) + return -EFAULT; + + return 0; +} + +static int sof_cdev_ioc_ipc_tx(struct sof_cdev_priv *priv, void __user *argp) +{ + struct sof_client_dev *cdev = priv->cdev; + size_t max_payload = sof_client_get_ipc_max_payload_size(cdev); + struct sof_cdev_ipc_msg req; + struct sof_ipc4_msg msg4 = {{ 0 }}; + struct sof_ipc4_msg reply4 = {{ 0 }}; + void *tx_buf = NULL; + void *rx_buf = NULL; + bool woke_dsp = false; + int ret; + + if (copy_from_user(&req, argp, sizeof(req))) + return -EFAULT; + + if (req.tx_size > max_payload || req.rx_size > max_payload) + return -EINVAL; + + if (req.tx_size) { + if (!req.tx_data_ptr) + return -EINVAL; + tx_buf = memdup_user(u64_to_user_ptr(req.tx_data_ptr), req.tx_size); + if (IS_ERR(tx_buf)) + return PTR_ERR(tx_buf); + } + + if (req.rx_size && !(req.flags & SOF_CDEV_IPC_FL_NO_REPLY)) { + if (!req.rx_data_ptr) { + kfree(tx_buf); + return -EINVAL; + } + rx_buf = kzalloc(req.rx_size, GFP_KERNEL); + if (!rx_buf) { + kfree(tx_buf); + return -ENOMEM; + } + } + + mutex_lock(&priv->io_lock); + + ret = sof_cdev_wake_dsp(cdev); + if (ret < 0) + goto out_unlock; + woke_dsp = true; + + if (sof_client_get_ipc_type(cdev) == SOF_IPC_TYPE_4) { + msg4.primary = req.primary; + msg4.extension = req.extension; + msg4.data_size = req.tx_size; + msg4.data_ptr = tx_buf; + + reply4.data_size = req.rx_size; + reply4.data_ptr = rx_buf; + + if (req.flags & SOF_CDEV_IPC_FL_NO_REPLY) + ret = sof_client_ipc_tx_message_no_reply(cdev, &msg4); + else + ret = sof_client_ipc_tx_message(cdev, &msg4, &reply4, + req.rx_size); + + req.reply_primary = reply4.primary; + req.reply_extension = reply4.extension; + req.ipc_status = (s32)(reply4.primary & SOF_IPC4_REPLY_STATUS); + req.rx_size = (u32)reply4.data_size; + } else { + if (!tx_buf || req.tx_size < sizeof(struct sof_ipc_cmd_hdr)) { + ret = -EINVAL; + goto out_unlock; + } + ret = sof_client_ipc_tx_message(cdev, tx_buf, rx_buf, req.rx_size); + req.ipc_status = ret; + } + +out_unlock: + if (woke_dsp) + sof_cdev_idle_dsp(cdev); + mutex_unlock(&priv->io_lock); + + if (!ret && rx_buf && req.rx_size > 0) { + if (copy_to_user(u64_to_user_ptr(req.rx_data_ptr), rx_buf, + req.rx_size)) + ret = -EFAULT; + } + + if (copy_to_user(argp, &req, sizeof(req))) + ret = ret ? ret : -EFAULT; + + kfree(rx_buf); + kfree(tx_buf); + return ret; +} + +static int sof_cdev_ioc_ipc_large_cfg(struct sof_cdev_priv *priv, void __user *argp) +{ + struct sof_client_dev *cdev = priv->cdev; + struct sof_cdev_large_cfg cfg; + struct sof_ipc4_msg msg = {{ 0 }}; + void *kbuf = NULL; + bool is_set; + int ret; + + if (sof_client_get_ipc_type(cdev) != SOF_IPC_TYPE_4) + return -EOPNOTSUPP; + + if (copy_from_user(&cfg, argp, sizeof(cfg))) + return -EFAULT; + + if (!cfg.data_size || cfg.data_size > SOF_CDEV_MAX_LARGE_CFG_BYTES || + !cfg.data_ptr) + return -EINVAL; + + is_set = (cfg.set_get == SOF_CDEV_LARGE_CFG_SET); + + if (is_set) { + kbuf = vmemdup_user(u64_to_user_ptr(cfg.data_ptr), cfg.data_size); + if (IS_ERR(kbuf)) + return PTR_ERR(kbuf); + } else { + kbuf = kvzalloc(cfg.data_size, GFP_KERNEL); + if (!kbuf) + return -ENOMEM; + } + + msg.primary = SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG) | + SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST) | + SOF_IPC4_MOD_ID(cfg.module_id) | + SOF_IPC4_MOD_INSTANCE(cfg.instance_id); + msg.extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(cfg.param_id); + msg.data_size = cfg.data_size; + msg.data_ptr = kbuf; + + mutex_lock(&priv->io_lock); + + ret = sof_cdev_wake_dsp(cdev); + if (!ret) { + ret = sof_client_ipc_set_get_data(cdev, &msg, is_set); + sof_cdev_idle_dsp(cdev); + } + + mutex_unlock(&priv->io_lock); + + if (!ret) { + cfg.data_size = (u32)msg.data_size; + if (!is_set && cfg.data_size > 0) { + if (copy_to_user(u64_to_user_ptr(cfg.data_ptr), kbuf, + cfg.data_size)) + ret = -EFAULT; + } + if (!ret && copy_to_user(argp, &cfg, sizeof(cfg))) + ret = -EFAULT; + } + + kvfree(kbuf); + return ret; +} + +static int sof_cdev_ioc_win_info(struct sof_cdev_priv *priv, void __user *argp) +{ + struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(priv->cdev); + struct sof_cdev_win_info *winfo; + int ret = 0; + + winfo = kzalloc_obj(*winfo); + if (!winfo) + return -ENOMEM; + + sof_cdev_fill_windows_and_slots(sdev, winfo->windows, winfo->debug_slots); + + if (copy_to_user(argp, winfo, sizeof(*winfo))) + ret = -EFAULT; + + kfree(winfo); + return ret; +} + +static int sof_cdev_ioc_win_io(struct sof_cdev_priv *priv, void __user *argp, + bool is_write) +{ + struct sof_client_dev *cdev = priv->cdev; + struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev); + struct sof_cdev_win_io wio; + u32 base_off = 0, max_size = 0; + void *kbuf = NULL; + bool woke_dsp = false; + bool got_active_ref = false; + int ret = 0; + + if (copy_from_user(&wio, argp, sizeof(wio))) + return -EFAULT; + + if (!wio.size || !wio.data_ptr) + return -EINVAL; + + mutex_lock(&priv->io_lock); + + if (wio.flags & SOF_CDEV_WIN_FL_WAKE_DSP) { + ret = sof_cdev_wake_dsp(cdev); + if (ret < 0) + goto out_unlock; + woke_dsp = true; + } else { + if (pm_runtime_get_if_active(sdev->dev) <= 0) { + ret = -EBUSY; + goto out_unlock; + } + got_active_ref = true; + if (!sof_cdev_is_dsp_d0i0(sdev)) { + ret = -EBUSY; + goto out_unlock; + } + } + + if (wio.win_id == SOF_CDEV_WIN_BY_SLOT_TYPE) { + ret = sof_cdev_resolve_slot(sdev, wio.slot_type, wio.slot_index, + &base_off, &max_size); + if (ret < 0) + goto out_unlock; + } else if (wio.win_id < SOF_CDEV_MAX_WINDOWS) { + int off = snd_sof_dsp_get_window_offset(sdev, wio.win_id); + + if (off < 0) { + ret = -EOPNOTSUPP; + goto out_unlock; + } + base_off = (u32)off; + max_size = sof_cdev_get_window_size(sdev, wio.win_id); + } else { + ret = -EINVAL; + goto out_unlock; + } + + if (wio.offset >= max_size || wio.size > max_size - wio.offset) { + ret = -EINVAL; + goto out_unlock; + } + + if (is_write) { + kbuf = memdup_user(u64_to_user_ptr(wio.data_ptr), wio.size); + if (IS_ERR(kbuf)) { + ret = PTR_ERR(kbuf); + kbuf = NULL; + goto out_unlock; + } + sof_mailbox_write(sdev, base_off + wio.offset, kbuf, wio.size); + } else { + kbuf = kzalloc(wio.size, GFP_KERNEL); + if (!kbuf) { + ret = -ENOMEM; + goto out_unlock; + } + sof_mailbox_read(sdev, base_off + wio.offset, kbuf, wio.size); + if (copy_to_user(u64_to_user_ptr(wio.data_ptr), kbuf, wio.size)) + ret = -EFAULT; + } + +out_unlock: + if (got_active_ref) + pm_runtime_put_noidle(sdev->dev); + if (woke_dsp) + sof_cdev_idle_dsp(cdev); + mutex_unlock(&priv->io_lock); + kfree(kbuf); + return ret; +} + +static int sof_cdev_ioc_winstream_io(struct sof_cdev_priv *priv, void __user *argp) +{ + struct sof_client_dev *cdev = priv->cdev; + struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev); + struct sof_cdev_winstream_io ws_io; + u32 slot_type, rx_win_size, tx_win_size; + u32 slot_off = 0, slot_sz = 0; + u8 *slot_buf = NULL; + u8 *user_tx = NULL; + u8 *user_rx = NULL; + bool woke_dsp = false; + int ret = 0; + + if (copy_from_user(&ws_io, argp, sizeof(ws_io))) + return -EFAULT; + + slot_type = ws_io.slot_type ? ws_io.slot_type : SOF_CDEV_SLOT_SHELL; + rx_win_size = ws_io.rx_win_size ? ws_io.rx_win_size : 256; + + if (rx_win_size < 64 || rx_win_size > 2048) + return -EINVAL; + + if (ws_io.tx_len > 0) { + if (!ws_io.tx_ptr || ws_io.tx_len > rx_win_size) + return -EINVAL; + user_tx = memdup_user(u64_to_user_ptr(ws_io.tx_ptr), ws_io.tx_len); + if (IS_ERR(user_tx)) + return PTR_ERR(user_tx); + } + + if (ws_io.rx_buf_len > 0) { + if (!ws_io.rx_ptr || ws_io.rx_buf_len > 16384) { + kfree(user_tx); + return -EINVAL; + } + user_rx = kzalloc(ws_io.rx_buf_len, GFP_KERNEL); + if (!user_rx) { + kfree(user_tx); + return -ENOMEM; + } + } + + slot_buf = kzalloc(SOF_IPC4_DEBUG_SLOT_SIZE, GFP_KERNEL); + if (!slot_buf) { + kfree(user_rx); + kfree(user_tx); + return -ENOMEM; + } + + mutex_lock(&priv->io_lock); + + ret = sof_cdev_wake_dsp(cdev); + if (ret < 0) + goto out_unlock; + woke_dsp = true; + + ret = sof_cdev_resolve_slot(sdev, slot_type, 0, &slot_off, &slot_sz); + if (ret < 0) + goto out_unlock; + + if (slot_sz <= rx_win_size + SOF_WINSTREAM_HDR_SIZE) { + ret = -EINVAL; + goto out_unlock; + } + tx_win_size = slot_sz - rx_win_size; + + if (user_tx && ws_io.tx_len > 0) { + sof_mailbox_read(sdev, slot_off, slot_buf, rx_win_size); + sof_ws_write((struct sof_winstream_hdr *)slot_buf, rx_win_size, + user_tx, ws_io.tx_len); + sof_mailbox_write(sdev, slot_off, slot_buf, rx_win_size); + } + + ws_io.rx_bytes_read = 0; + if (user_rx && ws_io.rx_buf_len > 0) { + sof_mailbox_read(sdev, slot_off + rx_win_size, slot_buf, tx_win_size); + ws_io.rx_bytes_read = sof_ws_read((struct sof_winstream_hdr *)slot_buf, + tx_win_size, &ws_io.seq_in_out, + user_rx, ws_io.rx_buf_len); + } + +out_unlock: + if (woke_dsp) + sof_cdev_idle_dsp(cdev); + mutex_unlock(&priv->io_lock); + + if (!ret && ws_io.rx_bytes_read > 0) { + if (copy_to_user(u64_to_user_ptr(ws_io.rx_ptr), user_rx, + ws_io.rx_bytes_read)) + ret = -EFAULT; + } + + if (!ret && copy_to_user(argp, &ws_io, sizeof(ws_io))) + ret = -EFAULT; + + kfree(slot_buf); + kfree(user_rx); + kfree(user_tx); + return ret; +} + +static int sof_cdev_ioc_llext_load(struct sof_cdev_priv *priv, void __user *argp) +{ + struct sof_client_dev *cdev = priv->cdev; + struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev); + struct sof_ipc4_fw_data *ipc4_data = sdev->private; + struct sof_cdev_llext_load req; + struct sof_shell_llext_slot slot; + u32 slot_off = 0, slot_sz = 0; + u32 lib_id; + void *buf; + bool woke_dsp = false; + int ret; + + if (sof_client_get_ipc_type(cdev) != SOF_IPC_TYPE_4 || !ipc4_data) + return -EOPNOTSUPP; + + if (copy_from_user(&req, argp, sizeof(req))) + return -EFAULT; + + if (!req.data_size || req.data_size > SOF_CDEV_MAX_LLEXT_BYTES || + !req.data_ptr) + return -EINVAL; + + mutex_lock(&priv->io_lock); + + ret = sof_cdev_wake_dsp(cdev); + if (ret < 0) + goto out_unlock; + woke_dsp = true; + + if (req.mode == SOF_LLEXT_MODE_SHELL_SHM) { + ret = sof_cdev_resolve_slot(sdev, SOF_CDEV_SLOT_LLEXT_LOAD, 0, + &slot_off, &slot_sz); + if (ret < 0) + goto out_unlock; + + sof_mailbox_read(sdev, slot_off, &slot, sizeof(slot)); + if (slot.magic != SOF_SHELL_LLEXT_MAGIC) { + ret = -EPROTO; + goto out_unlock; + } + if (slot.state != SOF_SHELL_LLEXT_REQUESTING) { + req.fw_state = slot.state; + ret = -EBUSY; + goto out_unlock; + } + + lib_id = slot.lib_id; + if (!lib_id || lib_id > 15) { + ret = -EINVAL; + goto out_shm_err; + } + + { + u32 active = SOF_SHELL_LLEXT_DMA_ACTIVE; + + sof_mailbox_write(sdev, + slot_off + offsetof(struct sof_shell_llext_slot, state), + &active, sizeof(active)); + } + + buf = devm_kmalloc(sdev->dev, req.data_size, GFP_KERNEL); + if (!buf) { + ret = -ENOMEM; + goto out_shm_err; + } + if (copy_from_user(buf, u64_to_user_ptr(req.data_ptr), req.data_size)) { + devm_kfree(sdev->dev, buf); + ret = -EFAULT; + goto out_shm_err; + } + + ret = sof_client_ipc4_load_library_buf(cdev, lib_id, buf, req.data_size); + if (ret < 0) { + devm_kfree(sdev->dev, buf); + goto out_shm_err; + } + + { + u32 xfer = req.data_size; + u32 done = SOF_SHELL_LLEXT_DMA_DONE; + + sof_mailbox_write(sdev, + slot_off + offsetof(struct sof_shell_llext_slot, xfer_bytes), + &xfer, sizeof(xfer)); + sof_mailbox_write(sdev, + slot_off + offsetof(struct sof_shell_llext_slot, state), + &done, sizeof(done)); + } + + req.lib_id = lib_id; + req.fw_state = SOF_SHELL_LLEXT_DMA_DONE; + goto out_copy; + +out_shm_err: + { + u32 err_state = SOF_SHELL_LLEXT_ERROR; + s32 result = (s32)ret; + + sof_mailbox_write(sdev, + slot_off + offsetof(struct sof_shell_llext_slot, result), + &result, sizeof(result)); + sof_mailbox_write(sdev, + slot_off + offsetof(struct sof_shell_llext_slot, state), + &err_state, sizeof(err_state)); + req.fw_state = SOF_SHELL_LLEXT_ERROR; + } + goto out_unlock; + } else if (req.mode == SOF_LLEXT_MODE_IPC4_DIRECT) { + lib_id = req.lib_id; + if (!lib_id) { + /* Find first free library slot >= 1 */ + u32 idx; + + for (idx = 1; idx < ipc4_data->max_libs_count; idx++) { + if (!xa_load(&ipc4_data->fw_lib_xa, idx)) { + lib_id = idx; + break; + } + } + if (!lib_id) + lib_id = 1; + } + + buf = devm_kmalloc(sdev->dev, req.data_size, GFP_KERNEL); + if (!buf) { + ret = -ENOMEM; + goto out_unlock; + } + if (copy_from_user(buf, u64_to_user_ptr(req.data_ptr), req.data_size)) { + devm_kfree(sdev->dev, buf); + ret = -EFAULT; + goto out_unlock; + } + + ret = sof_client_ipc4_load_library_buf(cdev, lib_id, buf, req.data_size); + if (ret < 0) + devm_kfree(sdev->dev, buf); + + req.lib_id = lib_id; + req.fw_state = ret; + } else { + ret = -EINVAL; + goto out_unlock; + } + +out_copy: + if (copy_to_user(argp, &req, sizeof(req))) + ret = ret ? ret : -EFAULT; + +out_unlock: + if (woke_dsp) + sof_cdev_idle_dsp(cdev); + mutex_unlock(&priv->io_lock); + return ret; +} + +static struct sof_man4_module *sof_cdev_get_probe_module(struct sof_cdev_priv *priv) +{ + static const guid_t probe_uuid = + GUID_INIT(0x7CAD0808, 0xAB10, 0xCD23, + 0xEF, 0x45, 0x12, 0xAB, 0x34, 0xCD, 0x56, 0xEF); + + if (!priv->probe_mentry) { + struct sof_ipc4_fw_module *fw_module = + sof_client_ipc4_find_module(priv->cdev, &probe_uuid); + + if (!fw_module) + return NULL; + priv->probe_mentry = &fw_module->man4_module_entry; + } + + return priv->probe_mentry; +} + +static int sof_cdev_ioc_probe_ctl(struct sof_cdev_priv *priv, void __user *argp) +{ + struct sof_client_dev *cdev = priv->cdev; + struct sof_cdev_probe_ctl pctl; + struct sof_man4_module *mentry; + struct sof_ipc4_msg msg = {{ 0 }}; + bool woke_dsp = false; + int i, ret = 0; + + if (sof_client_get_ipc_type(cdev) != SOF_IPC_TYPE_4) + return -EOPNOTSUPP; + + if (copy_from_user(&pctl, argp, sizeof(pctl))) + return -EFAULT; + + mutex_lock(&priv->io_lock); + + ret = sof_cdev_wake_dsp(cdev); + if (ret < 0) + goto out_unlock; + woke_dsp = true; + + mentry = sof_cdev_get_probe_module(priv); + if (!mentry) { + ret = -ENODEV; + goto out_unlock; + } + + switch (pctl.op) { + case SOF_PROBE_OP_INIT: { + struct sof_ipc4_probe_cfg cfg = {{ 0 }}; + + if (!pctl.stream_tag) { + ret = -EINVAL; + break; + } + cfg.gtw_cfg.node_id = SOF_IPC4_PROBE_NODE_ID_INDEX(pctl.stream_tag - 1) | + SOF_IPC4_PROBE_NODE_ID_TYPE(SOF_IPC4_DMA_HDA_HOST_INPUT); + cfg.gtw_cfg.dma_buffer_size = pctl.dma_buffer_size; + + msg.primary = mentry->id | + SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_MOD_INIT_INSTANCE) | + SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST) | + SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG); + msg.extension = SOF_IPC4_MOD_EXT_DST_MOD_INSTANCE(SOF_PROBE_INVALID_PIPELINE_ID) | + SOF_IPC4_MOD_EXT_CORE_ID(0) | + SOF_IPC4_MOD_EXT_PARAM_SIZE(sizeof(cfg) / sizeof(u32)); + msg.data_size = sizeof(cfg); + msg.data_ptr = &cfg; + + ret = sof_client_ipc_tx_message_no_reply(cdev, &msg); + break; + } + case SOF_PROBE_OP_DEINIT: + msg.primary = mentry->id | + SOF_IPC4_MSG_TYPE_SET(SOF_IPC4_MOD_DELETE_INSTANCE) | + SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST) | + SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG); + msg.extension = SOF_IPC4_MOD_EXT_DST_MOD_INSTANCE(SOF_PROBE_INVALID_PIPELINE_ID) | + SOF_IPC4_MOD_EXT_CORE_ID(0); + ret = sof_client_ipc_tx_message_no_reply(cdev, &msg); + break; + + case SOF_PROBE_OP_INFO_ACTIVE: + case SOF_PROBE_OP_INFO_AVAILABLE: { + u32 param_id = (pctl.op == SOF_PROBE_OP_INFO_ACTIVE) ? + SOF_IPC4_PROBE_POINTS : SOF_IPC4_PROBE_POINTS_AVAILABLE; + struct sof_ipc4_probe_info *info; + struct sof_cdev_probe_point *upoints = NULL; + u32 count, to_copy; + + msg.primary = mentry->id | + SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST) | + SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG); + msg.extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(param_id); + msg.data_size = sof_client_get_ipc_max_payload_size(cdev); + msg.data_ptr = kzalloc(msg.data_size, GFP_KERNEL); + if (!msg.data_ptr) { + ret = -ENOMEM; + break; + } + + ret = sof_client_ipc_set_get_data(cdev, &msg, false); + if (ret < 0) { + kfree(msg.data_ptr); + break; + } + + info = msg.data_ptr; + if (msg.data_size < sizeof(*info) || + info->num_elems > (msg.data_size - sizeof(*info)) / + sizeof(info->points[0])) { + kfree(msg.data_ptr); + ret = -EINVAL; + break; + } + + count = info->num_elems; + to_copy = min(count, pctl.num_points); + pctl.num_points = count; + + if (to_copy > 0 && pctl.points_ptr) { + upoints = kcalloc(to_copy, sizeof(*upoints), GFP_KERNEL); + if (!upoints) { + kfree(msg.data_ptr); + ret = -ENOMEM; + break; + } + for (i = 0; i < to_copy; i++) { + u32 bid = info->points[i].point_id; + struct snd_sof_widget *swidget; + + upoints[i].buffer_id = bid; + upoints[i].purpose = info->points[i].purpose; + upoints[i].stream_tag = info->points[i].stream_tag; + + swidget = sof_client_ipc4_find_swidget_by_id( + cdev, + SOF_IPC4_MOD_ID_GET(bid), + SOF_IPC4_MOD_INSTANCE_GET(bid)); + if (swidget && swidget->widget && swidget->widget->name) + strscpy(upoints[i].widget_name, + swidget->widget->name, + sizeof(upoints[i].widget_name)); + else + strscpy(upoints[i].widget_name, "", + sizeof(upoints[i].widget_name)); + } + + if (copy_to_user(u64_to_user_ptr(pctl.points_ptr), + upoints, to_copy * sizeof(*upoints))) + ret = -EFAULT; + kfree(upoints); + } + kfree(msg.data_ptr); + break; + } + case SOF_PROBE_OP_POINTS_ADD: { + struct sof_cdev_probe_point *upoints; + struct sof_ipc4_probe_point *kpoints; + + if (!pctl.num_points || pctl.num_points > 64 || !pctl.points_ptr) { + ret = -EINVAL; + break; + } + upoints = memdup_user(u64_to_user_ptr(pctl.points_ptr), + pctl.num_points * sizeof(*upoints)); + if (IS_ERR(upoints)) { + ret = PTR_ERR(upoints); + break; + } + kpoints = kcalloc(pctl.num_points, sizeof(*kpoints), GFP_KERNEL); + if (!kpoints) { + kfree(upoints); + ret = -ENOMEM; + break; + } + for (i = 0; i < pctl.num_points; i++) { + kpoints[i].point_id = upoints[i].buffer_id; + kpoints[i].purpose = upoints[i].purpose; + kpoints[i].stream_tag = upoints[i].stream_tag; + } + + msg.primary = mentry->id | + SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST) | + SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG); + msg.extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(SOF_IPC4_PROBE_POINTS); + msg.data_size = pctl.num_points * sizeof(*kpoints); + msg.data_ptr = kpoints; + + ret = sof_client_ipc_set_get_data(cdev, &msg, true); + kfree(kpoints); + kfree(upoints); + break; + } + case SOF_PROBE_OP_POINTS_REMOVE: { + struct sof_cdev_probe_point *upoints; + u32 *ids; + + if (!pctl.num_points || pctl.num_points > 64 || !pctl.points_ptr) { + ret = -EINVAL; + break; + } + upoints = memdup_user(u64_to_user_ptr(pctl.points_ptr), + pctl.num_points * sizeof(*upoints)); + if (IS_ERR(upoints)) { + ret = PTR_ERR(upoints); + break; + } + ids = kcalloc(pctl.num_points, sizeof(*ids), GFP_KERNEL); + if (!ids) { + kfree(upoints); + ret = -ENOMEM; + break; + } + for (i = 0; i < pctl.num_points; i++) + ids[i] = upoints[i].buffer_id; + + msg.primary = mentry->id | + SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST) | + SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG); + msg.extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(SOF_IPC4_PROBE_POINTS_DISCONNECT); + msg.data_size = pctl.num_points * sizeof(*ids); + msg.data_ptr = ids; + + ret = sof_client_ipc_set_get_data(cdev, &msg, true); + kfree(ids); + kfree(upoints); + break; + } + default: + ret = -EINVAL; + break; + } + +out_unlock: + if (woke_dsp) + sof_cdev_idle_dsp(cdev); + mutex_unlock(&priv->io_lock); + + if (!ret && copy_to_user(argp, &pctl, sizeof(pctl))) + ret = -EFAULT; + + return ret; +} + +/* ------------------------------------------------------------------------- + * Character Device File Operations + * ------------------------------------------------------------------------- + */ + +static int sof_cdev_open(struct inode *inode, struct file *file) +{ + struct miscdevice *misc = file->private_data; + struct sof_cdev_priv *priv = container_of(misc, struct sof_cdev_priv, miscdev); + struct sof_cdev_fh *fh; + + fh = kzalloc_obj(*fh); + if (!fh) + return -ENOMEM; + + fh->priv = priv; + init_waitqueue_head(&fh->wait); + spin_lock_init(&fh->fifo_lock); + INIT_KFIFO(fh->events); + + mutex_lock(&priv->fh_lock); + list_add(&fh->list, &priv->fh_list); + mutex_unlock(&priv->fh_lock); + + file->private_data = fh; + return 0; +} + +static int sof_cdev_release(struct inode *inode, struct file *file) +{ + struct sof_cdev_fh *fh = file->private_data; + struct sof_cdev_priv *priv = fh->priv; + + mutex_lock(&priv->fh_lock); + list_del(&fh->list); + mutex_unlock(&priv->fh_lock); + + mutex_lock(&priv->io_lock); + if (fh->holding_d0) { + sof_cdev_idle_dsp(priv->cdev); + sof_client_core_module_put(priv->cdev); + fh->holding_d0 = false; + } + mutex_unlock(&priv->io_lock); + + kfree(fh); + return 0; +} + +static long sof_cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg) +{ + struct sof_cdev_fh *fh = file->private_data; + struct sof_cdev_priv *priv = fh->priv; + void __user *argp = (void __user *)arg; + + switch (cmd) { + case SOF_IOC_GET_INFO: + return sof_cdev_ioc_get_info(priv, argp); + case SOF_IOC_PM_CTL: + return sof_cdev_ioc_pm_ctl(fh, argp); + case SOF_IOC_IPC_TX: + return sof_cdev_ioc_ipc_tx(priv, argp); + case SOF_IOC_IPC_LARGE_CFG: + return sof_cdev_ioc_ipc_large_cfg(priv, argp); + case SOF_IOC_WIN_INFO: + return sof_cdev_ioc_win_info(priv, argp); + case SOF_IOC_WIN_READ: + return sof_cdev_ioc_win_io(priv, argp, false); + case SOF_IOC_WIN_WRITE: + return sof_cdev_ioc_win_io(priv, argp, true); + case SOF_IOC_WINSTREAM_IO: + return sof_cdev_ioc_winstream_io(priv, argp); + case SOF_IOC_LLEXT_LOAD: + return sof_cdev_ioc_llext_load(priv, argp); + case SOF_IOC_PROBE_CTL: + return sof_cdev_ioc_probe_ctl(priv, argp); + default: + return -ENOTTY; + } +} + +static int sof_cdev_mmap(struct file *file, struct vm_area_struct *vma) +{ + struct sof_cdev_fh *fh = file->private_data; + struct sof_cdev_priv *priv = fh->priv; + struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(priv->cdev); + unsigned long size = vma->vm_end - vma->vm_start; + u32 win_id = (vma->vm_pgoff >> (20 - PAGE_SHIFT)) & 0x7; + u32 sub_pgoff = vma->vm_pgoff & ((1UL << (20 - PAGE_SHIFT)) - 1); + u32 max_size; + int win_off; + phys_addr_t bar_phys, win_phys; + + if (!dev_is_pci(sdev->dev)) + return -EOPNOTSUPP; + + if (win_id >= SOF_CDEV_MAX_WINDOWS) + return -EINVAL; + + win_off = snd_sof_dsp_get_window_offset(sdev, win_id); + if (win_off < 0) + return -EOPNOTSUPP; + + max_size = sof_cdev_get_window_size(sdev, win_id); + + if ((sub_pgoff << PAGE_SHIFT) >= max_size || + size > max_size - (sub_pgoff << PAGE_SHIFT)) + return -EINVAL; + + bar_phys = pci_resource_start(to_pci_dev(sdev->dev), sdev->mailbox_bar); + if (!bar_phys) + return -ENXIO; + + win_phys = bar_phys + (u32)win_off + (sub_pgoff << PAGE_SHIFT); + vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); + + return io_remap_pfn_range(vma, vma->vm_start, win_phys >> PAGE_SHIFT, + size, vma->vm_page_prot); +} + +static __poll_t sof_cdev_poll(struct file *file, poll_table *wait) +{ + struct sof_cdev_fh *fh = file->private_data; + __poll_t mask = 0; + + poll_wait(file, &fh->wait, wait); + + if (!kfifo_is_empty(&fh->events)) + mask |= EPOLLIN | EPOLLRDNORM; + + return mask; +} + +static ssize_t sof_cdev_read(struct file *file, char __user *buf, size_t count, + loff_t *ppos) +{ + struct sof_cdev_fh *fh = file->private_data; + struct sof_cdev_event evt; + size_t copied = 0; + int ret; + + if (count < sizeof(evt)) + return -EINVAL; + + if (kfifo_is_empty(&fh->events)) { + if (file->f_flags & O_NONBLOCK) + return -EAGAIN; + ret = wait_event_interruptible(fh->wait, + !kfifo_is_empty(&fh->events)); + if (ret) + return ret; + } + + while (copied + sizeof(evt) <= count && + kfifo_out_spinlocked(&fh->events, &evt, 1, &fh->fifo_lock)) { + if (copy_to_user(buf + copied, &evt, sizeof(evt))) + return copied ? (ssize_t)copied : -EFAULT; + copied += sizeof(evt); + } + + return copied; +} + +static const struct file_operations sof_cdev_fops = { + .owner = THIS_MODULE, + .open = sof_cdev_open, + .release = sof_cdev_release, + .unlocked_ioctl = sof_cdev_ioctl, + .compat_ioctl = compat_ptr_ioctl, + .mmap = sof_cdev_mmap, + .poll = sof_cdev_poll, + .read = sof_cdev_read, + .llseek = noop_llseek, +}; + +/* ------------------------------------------------------------------------- + * Async FW State & Notification Callbacks + * ------------------------------------------------------------------------- + */ + +static void sof_cdev_broadcast_event(struct sof_cdev_priv *priv, + const struct sof_cdev_event *evt) +{ + struct sof_cdev_fh *fh; + + mutex_lock(&priv->fh_lock); + list_for_each_entry(fh, &priv->fh_list, list) { + unsigned long flags; + + spin_lock_irqsave(&fh->fifo_lock, flags); + if (kfifo_is_full(&fh->events)) { + struct sof_cdev_event dummy; + int __maybe_unused n = kfifo_out(&fh->events, &dummy, 1); + } + kfifo_in(&fh->events, evt, 1); + spin_unlock_irqrestore(&fh->fifo_lock, flags); + + wake_up_interruptible(&fh->wait); + } + mutex_unlock(&priv->fh_lock); +} + +static void sof_cdev_fw_state_cb(struct sof_client_dev *cdev, + enum sof_fw_state state) +{ + struct sof_cdev_priv *priv = cdev->data; + struct sof_cdev_event evt = { + .timestamp_ns = ktime_get_ns(), + .event_type = SOF_CDEV_EVT_FW_STATE, + .fw_state = state, + }; + + if (state == SOF_FW_CRASHED || state == SOF_FW_BOOT_NOT_STARTED) + priv->probe_mentry = NULL; + + sof_cdev_broadcast_event(priv, &evt); +} + +static void sof_cdev_ipc_notif_cb(struct sof_client_dev *cdev, void *msg_buf) +{ + struct sof_cdev_priv *priv = cdev->data; + struct sof_ipc4_msg *msg = msg_buf; + struct sof_cdev_event evt = { + .timestamp_ns = ktime_get_ns(), + .event_type = SOF_CDEV_EVT_IPC_NOTIFICATION, + .fw_state = sof_client_get_fw_state(cdev), + .primary = msg ? msg->primary : 0, + .extension = msg ? msg->extension : 0, + }; + + sof_cdev_broadcast_event(priv, &evt); +} + +/* ------------------------------------------------------------------------- + * Auxiliary Driver Probe / Remove + * ------------------------------------------------------------------------- + */ + +static int sof_cdev_probe(struct auxiliary_device *auxdev, + const struct auxiliary_device_id *id) +{ + struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev); + struct sof_cdev_priv *priv; + int ret; + + priv = devm_kzalloc(&auxdev->dev, sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + + priv->cdev = cdev; + mutex_init(&priv->io_lock); + mutex_init(&priv->fh_lock); + INIT_LIST_HEAD(&priv->fh_list); + cdev->data = priv; + + snprintf(priv->dev_name, sizeof(priv->dev_name), "sof-dsp%d", auxdev->id); + priv->miscdev.minor = MISC_DYNAMIC_MINOR; + priv->miscdev.name = priv->dev_name; + priv->miscdev.fops = &sof_cdev_fops; + priv->miscdev.parent = &auxdev->dev; + priv->miscdev.mode = 0666; + + ret = misc_register(&priv->miscdev); + if (ret < 0) + return ret; + + sof_client_register_fw_state_handler(cdev, sof_cdev_fw_state_cb); + + if (sof_client_get_ipc_type(cdev) == SOF_IPC_TYPE_4) { + sof_client_register_ipc_rx_handler(cdev, + SOF_IPC4_NOTIFY_RESOURCE_EVENT, sof_cdev_ipc_notif_cb); + sof_client_register_ipc_rx_handler(cdev, + SOF_IPC4_NOTIFY_EXCEPTION_CAUGHT, sof_cdev_ipc_notif_cb); + sof_client_register_ipc_rx_handler(cdev, + SOF_IPC4_NOTIFY_PHRASE_DETECTED, sof_cdev_ipc_notif_cb); + } + + pm_runtime_set_autosuspend_delay(&auxdev->dev, SOF_CDEV_SUSPEND_DELAY_MS); + pm_runtime_use_autosuspend(&auxdev->dev); + pm_runtime_enable(&auxdev->dev); + pm_runtime_mark_last_busy(&auxdev->dev); + pm_runtime_idle(&auxdev->dev); + + dev_info(&auxdev->dev, "Registered SOF character device /dev/%s\n", + priv->dev_name); + return 0; +} + +static void sof_cdev_remove(struct auxiliary_device *auxdev) +{ + struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev); + struct sof_cdev_priv *priv = cdev->data; + + pm_runtime_disable(&auxdev->dev); + + if (sof_client_get_ipc_type(cdev) == SOF_IPC_TYPE_4) { + sof_client_unregister_ipc_rx_handler(cdev, + SOF_IPC4_NOTIFY_PHRASE_DETECTED); + sof_client_unregister_ipc_rx_handler(cdev, + SOF_IPC4_NOTIFY_EXCEPTION_CAUGHT); + sof_client_unregister_ipc_rx_handler(cdev, + SOF_IPC4_NOTIFY_RESOURCE_EVENT); + } + + sof_client_unregister_fw_state_handler(cdev); + misc_deregister(&priv->miscdev); +} + +static const struct auxiliary_device_id sof_cdev_id_table[] = { + { .name = "snd_sof.cdev" }, + { }, +}; +MODULE_DEVICE_TABLE(auxiliary, sof_cdev_id_table); + +static struct auxiliary_driver sof_cdev_drv = { + .name = "snd_sof_client_cdev", + .probe = sof_cdev_probe, + .remove = sof_cdev_remove, + .id_table = sof_cdev_id_table, +}; + +module_auxiliary_driver(sof_cdev_drv); + +MODULE_DESCRIPTION("SOF Client Character Device Driver (/dev/sof-dspN)"); +MODULE_LICENSE("GPL"); +MODULE_IMPORT_NS("SND_SOC_SOF_CLIENT"); +MODULE_IMPORT_NS("SND_SOC_SOF"); diff --git a/sound/soc/sof/sof-client.c b/sound/soc/sof/sof-client.c index 6b4d87efa65319..2c448c1814dae2 100644 --- a/sound/soc/sof/sof-client.c +++ b/sound/soc/sof/sof-client.c @@ -186,6 +186,25 @@ static inline int sof_register_fw_gdb(struct snd_sof_dev *sdev) static inline void sof_unregister_fw_gdb(struct snd_sof_dev *sdev) {} #endif /* CONFIG_SND_SOC_SOF_DEBUG_FW_GDB */ +#if IS_ENABLED(CONFIG_SND_SOC_SOF_CLIENT_CDEV) +static int sof_register_cdev(struct snd_sof_dev *sdev) +{ + return sof_client_dev_register(sdev, "cdev", 0, NULL, 0); +} + +static void sof_unregister_cdev(struct snd_sof_dev *sdev) +{ + sof_client_dev_unregister(sdev, "cdev", 0); +} +#else +static inline int sof_register_cdev(struct snd_sof_dev *sdev) +{ + return 0; +} + +static inline void sof_unregister_cdev(struct snd_sof_dev *sdev) {} +#endif /* CONFIG_SND_SOC_SOF_CLIENT_CDEV */ + int sof_register_clients(struct snd_sof_dev *sdev) { int ret; @@ -218,6 +237,12 @@ int sof_register_clients(struct snd_sof_dev *sdev) goto err_fw_gdb; } + ret = sof_register_cdev(sdev); + if (ret) { + dev_err(sdev->dev, "SOF char device client registration failed\n"); + goto err_cdev; + } + /* Platform dependent client device registration */ if (sof_ops(sdev) && sof_ops(sdev)->register_ipc_clients) @@ -226,11 +251,14 @@ int sof_register_clients(struct snd_sof_dev *sdev) if (!ret) return 0; - sof_unregister_ipc_kernel_injector(sdev); + sof_unregister_cdev(sdev); -err_fw_gdb: +err_cdev: sof_unregister_fw_gdb(sdev); +err_fw_gdb: + sof_unregister_ipc_kernel_injector(sdev); + err_kernel_injector: sof_unregister_ipc_msg_injector(sdev); @@ -245,6 +273,7 @@ void sof_unregister_clients(struct snd_sof_dev *sdev) if (sof_ops(sdev) && sof_ops(sdev)->unregister_ipc_clients) sof_ops(sdev)->unregister_ipc_clients(sdev); + sof_unregister_cdev(sdev); sof_unregister_ipc_kernel_injector(sdev); sof_unregister_ipc_msg_injector(sdev); sof_unregister_ipc_flood_test(sdev); @@ -311,24 +340,29 @@ EXPORT_SYMBOL_NS_GPL(sof_client_dev_register, "SND_SOC_SOF_CLIENT"); void sof_client_dev_unregister(struct snd_sof_dev *sdev, const char *name, u32 id) { - struct sof_client_dev_entry *centry; - - guard(mutex)(&sdev->ipc_client_mutex); + struct sof_client_dev_entry *centry = NULL; + struct sof_client_dev_entry *pos; /* * sof_client_auxdev_release() will be invoked to free up memory * allocations through put_device() */ - list_for_each_entry(centry, &sdev->ipc_client_list, list) { - struct sof_client_dev *cdev = ¢ry->client_dev; - - if (!strcmp(cdev->auxdev.name, name) && cdev->auxdev.id == id) { - list_del(¢ry->list); - auxiliary_device_delete(&cdev->auxdev); - auxiliary_device_uninit(&cdev->auxdev); - break; + scoped_guard(mutex, &sdev->ipc_client_mutex) { + list_for_each_entry(pos, &sdev->ipc_client_list, list) { + if (!strcmp(pos->client_dev.auxdev.name, name) && + pos->client_dev.auxdev.id == id) { + list_del(&pos->list); + centry = pos; + break; + } } } + + if (!centry) + return; + + auxiliary_device_delete(¢ry->client_dev.auxdev); + auxiliary_device_uninit(¢ry->client_dev.auxdev); } EXPORT_SYMBOL_NS_GPL(sof_client_dev_unregister, "SND_SOC_SOF_CLIENT"); @@ -426,6 +460,14 @@ ssize_t sof_client_ipc4_find_debug_slot_offset_by_type(struct sof_client_dev *cd return sof_ipc4_find_debug_slot_offset_by_type(sof_client_dev_to_sof_dev(cdev), type); } EXPORT_SYMBOL_NS_GPL(sof_client_ipc4_find_debug_slot_offset_by_type, "SND_SOC_SOF_CLIENT"); + +int sof_client_ipc4_load_library_buf(struct sof_client_dev *cdev, u32 lib_id, + const void *buf, size_t size) +{ + return snd_sof_ipc4_load_library_from_buf(sof_client_dev_to_sof_dev(cdev), + lib_id, buf, size); +} +EXPORT_SYMBOL_NS_GPL(sof_client_ipc4_load_library_buf, "SND_SOC_SOF_CLIENT"); #endif int sof_suspend_clients(struct snd_sof_dev *sdev, pm_message_t state) diff --git a/sound/soc/sof/sof-client.h b/sound/soc/sof/sof-client.h index e796801570c823..178fb479a99d24 100644 --- a/sound/soc/sof/sof-client.h +++ b/sound/soc/sof/sof-client.h @@ -83,5 +83,7 @@ void sof_client_mailbox_write(struct sof_client_dev *cdev, u32 offset, ssize_t sof_client_ipc4_find_debug_slot_offset_by_type(struct sof_client_dev *cdev, u32 type); +int sof_client_ipc4_load_library_buf(struct sof_client_dev *cdev, u32 lib_id, + const void *buf, size_t size); #endif /* __SOC_SOF_CLIENT_H */