1 /* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
6 * Copyright(c) 2018 Intel Corporation. All rights reserved.
8 * Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
11 #ifndef __SOUND_SOC_SOF_PRIV_H
12 #define __SOUND_SOC_SOF_PRIV_H
14 #include <linux/device.h>
16 #include <sound/hdaudio.h>
17 #include <sound/soc.h>
19 #include <sound/sof.h>
20 #include <sound/sof/stream.h> /* needs to be included before control.h */
21 #include <sound/sof/control.h>
22 #include <sound/sof/dai.h>
23 #include <sound/sof/info.h>
24 #include <sound/sof/pm.h>
25 #include <sound/sof/topology.h>
26 #include <sound/sof/trace.h>
28 #include <uapi/sound/sof/fw.h>
31 #define SOF_DBG_ENABLE_TRACE BIT(0)
32 #define SOF_DBG_REGS BIT(1)
33 #define SOF_DBG_MBOX BIT(2)
34 #define SOF_DBG_TEXT BIT(3)
35 #define SOF_DBG_PCI BIT(4)
36 #define SOF_DBG_RETAIN_CTX BIT(5) /* prevent DSP D3 on FW exception */
38 /* global debug state set by SOF_DBG_ flags */
39 extern int sof_core_debug;
41 /* max BARs mmaped devices can use */
42 #define SND_SOF_BARS 8
44 /* time in ms for runtime suspend delay */
45 #define SND_SOF_SUSPEND_DELAY_MS 2000
47 /* DMA buffer size for trace */
48 #define DMA_BUF_SIZE_FOR_TRACE (PAGE_SIZE * 16)
50 /* max number of FE PCMs before BEs */
51 #define SOF_BE_PCM_BASE 16
53 #define SOF_IPC_DSP_REPLY 0
54 #define SOF_IPC_HOST_REPLY 1
56 /* convenience constructor for DAI driver streams */
57 #define SOF_DAI_STREAM(sname, scmin, scmax, srates, sfmt) \
58 {.stream_name = sname, .channels_min = scmin, .channels_max = scmax, \
59 .rates = srates, .formats = sfmt}
61 #define SOF_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE | \
62 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_FLOAT)
64 #define ENABLE_DEBUGFS_CACHEBUF \
65 (IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE) || \
66 IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_IPC_FLOOD_TEST))
68 #define DMA_CHAN_INVALID 0xFFFFFFFF
71 struct snd_sof_ipc_msg;
73 struct snd_sof_debugfs_map;
74 struct snd_soc_tplg_ops;
75 struct snd_soc_component;
79 * SOF DSP HW abstraction operations.
80 * Used to abstract DSP HW architecture and any IO busses between host CPU
83 struct snd_sof_dsp_ops {
85 /* probe and remove */
86 int (*probe)(struct snd_sof_dev *sof_dev); /* mandatory */
87 int (*remove)(struct snd_sof_dev *sof_dev); /* optional */
89 /* DSP core boot / reset */
90 int (*run)(struct snd_sof_dev *sof_dev); /* mandatory */
91 int (*stall)(struct snd_sof_dev *sof_dev); /* optional */
92 int (*reset)(struct snd_sof_dev *sof_dev); /* optional */
93 int (*core_power_up)(struct snd_sof_dev *sof_dev,
94 unsigned int core_mask); /* optional */
95 int (*core_power_down)(struct snd_sof_dev *sof_dev,
96 unsigned int core_mask); /* optional */
99 * Register IO: only used by respective drivers themselves,
100 * TODO: consider removing these operations and calling respective
101 * implementations directly
103 void (*write)(struct snd_sof_dev *sof_dev, void __iomem *addr,
104 u32 value); /* optional */
105 u32 (*read)(struct snd_sof_dev *sof_dev,
106 void __iomem *addr); /* optional */
107 void (*write64)(struct snd_sof_dev *sof_dev, void __iomem *addr,
108 u64 value); /* optional */
109 u64 (*read64)(struct snd_sof_dev *sof_dev,
110 void __iomem *addr); /* optional */
113 void (*block_read)(struct snd_sof_dev *sof_dev, u32 bar,
114 u32 offset, void *dest,
115 size_t size); /* mandatory */
116 void (*block_write)(struct snd_sof_dev *sof_dev, u32 bar,
117 u32 offset, void *src,
118 size_t size); /* mandatory */
121 irqreturn_t (*irq_handler)(int irq, void *context); /* optional */
122 irqreturn_t (*irq_thread)(int irq, void *context); /* optional */
125 int (*send_msg)(struct snd_sof_dev *sof_dev,
126 struct snd_sof_ipc_msg *msg); /* mandatory */
129 int (*load_firmware)(struct snd_sof_dev *sof_dev); /* mandatory */
130 int (*load_module)(struct snd_sof_dev *sof_dev,
131 struct snd_sof_mod_hdr *hdr); /* optional */
133 * FW ready checks for ABI compatibility and creates
134 * memory windows at first boot
136 int (*fw_ready)(struct snd_sof_dev *sdev, u32 msg_id); /* mandatory */
138 /* connect pcm substream to a host stream */
139 int (*pcm_open)(struct snd_sof_dev *sdev,
140 struct snd_pcm_substream *substream); /* optional */
141 /* disconnect pcm substream to a host stream */
142 int (*pcm_close)(struct snd_sof_dev *sdev,
143 struct snd_pcm_substream *substream); /* optional */
145 /* host stream hw params */
146 int (*pcm_hw_params)(struct snd_sof_dev *sdev,
147 struct snd_pcm_substream *substream,
148 struct snd_pcm_hw_params *params,
149 struct sof_ipc_stream_params *ipc_params); /* optional */
151 /* host stream hw_free */
152 int (*pcm_hw_free)(struct snd_sof_dev *sdev,
153 struct snd_pcm_substream *substream); /* optional */
155 /* host stream trigger */
156 int (*pcm_trigger)(struct snd_sof_dev *sdev,
157 struct snd_pcm_substream *substream,
158 int cmd); /* optional */
160 /* host stream pointer */
161 snd_pcm_uframes_t (*pcm_pointer)(struct snd_sof_dev *sdev,
162 struct snd_pcm_substream *substream); /* optional */
164 /* host read DSP stream data */
165 void (*ipc_msg_data)(struct snd_sof_dev *sdev,
166 struct snd_pcm_substream *substream,
167 void *p, size_t sz); /* mandatory */
169 /* host configure DSP HW parameters */
170 int (*ipc_pcm_params)(struct snd_sof_dev *sdev,
171 struct snd_pcm_substream *substream,
172 const struct sof_ipc_pcm_params_reply *reply); /* mandatory */
174 /* pre/post firmware run */
175 int (*pre_fw_run)(struct snd_sof_dev *sof_dev); /* optional */
176 int (*post_fw_run)(struct snd_sof_dev *sof_dev); /* optional */
179 int (*suspend)(struct snd_sof_dev *sof_dev); /* optional */
180 int (*resume)(struct snd_sof_dev *sof_dev); /* optional */
181 int (*runtime_suspend)(struct snd_sof_dev *sof_dev); /* optional */
182 int (*runtime_resume)(struct snd_sof_dev *sof_dev); /* optional */
183 int (*runtime_idle)(struct snd_sof_dev *sof_dev); /* optional */
184 int (*set_hw_params_upon_resume)(struct snd_sof_dev *sdev); /* optional */
187 int (*set_clk)(struct snd_sof_dev *sof_dev, u32 freq); /* optional */
190 const struct snd_sof_debugfs_map *debug_map; /* optional */
191 int debug_map_count; /* optional */
192 void (*dbg_dump)(struct snd_sof_dev *sof_dev,
193 u32 flags); /* optional */
194 void (*ipc_dump)(struct snd_sof_dev *sof_dev); /* optional */
196 /* host DMA trace initialization */
197 int (*trace_init)(struct snd_sof_dev *sdev,
198 u32 *stream_tag); /* optional */
199 int (*trace_release)(struct snd_sof_dev *sdev); /* optional */
200 int (*trace_trigger)(struct snd_sof_dev *sdev,
201 int cmd); /* optional */
204 int (*get_bar_index)(struct snd_sof_dev *sdev,
205 u32 type); /* optional */
206 int (*get_mailbox_offset)(struct snd_sof_dev *sdev);/* mandatory for common loader code */
207 int (*get_window_offset)(struct snd_sof_dev *sdev,
208 u32 id);/* mandatory for common loader code */
211 struct snd_soc_dai_driver *drv;
215 /* DSP architecture specific callbacks for oops and stack dumps */
216 struct sof_arch_ops {
217 void (*dsp_oops)(struct snd_sof_dev *sdev, void *oops);
218 void (*dsp_stack)(struct snd_sof_dev *sdev, void *oops,
219 u32 *stack, u32 stack_words);
222 #define sof_arch_ops(sdev) ((sdev)->pdata->desc->arch_ops)
224 /* DSP device HW descriptor mapping between bus ID and ops */
225 struct sof_ops_table {
226 const struct sof_dev_desc *desc;
227 const struct snd_sof_dsp_ops *ops;
230 enum sof_dfsentry_type {
231 SOF_DFSENTRY_TYPE_IOMEM = 0,
232 SOF_DFSENTRY_TYPE_BUF,
235 enum sof_debugfs_access_type {
236 SOF_DEBUGFS_ACCESS_ALWAYS = 0,
237 SOF_DEBUGFS_ACCESS_D0_ONLY,
240 /* FS entry for debug files that can expose DSP memories, registers */
241 struct snd_sof_dfsentry {
243 enum sof_dfsentry_type type;
245 * access_type specifies if the
246 * memory -> DSP resource (memory, register etc) is always accessible
247 * or if it is accessible only when the DSP is in D0.
249 enum sof_debugfs_access_type access_type;
250 #if ENABLE_DEBUGFS_CACHEBUF
251 char *cache_buf; /* buffer to cache the contents of debugfs memory */
253 struct snd_sof_dev *sdev;
254 struct list_head list; /* list in sdev dfsentry list */
256 void __iomem *io_mem;
261 /* Debug mapping for any DSP memory or registers that can used for debug */
262 struct snd_sof_debugfs_map {
268 * access_type specifies if the memory is always accessible
269 * or if it is accessible only when the DSP is in D0.
271 enum sof_debugfs_access_type access_type;
274 /* mailbox descriptor, used for host <-> DSP IPC */
275 struct snd_sof_mailbox {
280 /* IPC message descriptor for host <-> DSP IO */
281 struct snd_sof_ipc_msg {
290 wait_queue_head_t waitq;
294 /* PCM stream, mapped to FW component */
295 struct snd_sof_pcm_stream {
297 struct snd_dma_buffer page_table;
298 struct sof_ipc_stream_posn posn;
299 struct snd_pcm_substream *substream;
300 struct work_struct period_elapsed_work;
303 /* ALSA SOF PCM device */
305 struct snd_sof_dev *sdev;
306 struct snd_soc_tplg_pcm pcm;
307 struct snd_sof_pcm_stream stream[2];
308 struct list_head list; /* list in sdev pcm list */
309 struct snd_pcm_hw_params params[2];
310 bool prepared[2]; /* PCM_PARAMS set successfully */
313 /* ALSA SOF Kcontrol device */
314 struct snd_sof_control {
315 struct snd_sof_dev *sdev;
317 int min_volume_step; /* min volume step for volume_table */
318 int max_volume_step; /* max volume step for volume_table */
320 u32 readback_offset; /* offset to mmaped data if used */
321 struct sof_ipc_ctrl_data *control_data;
322 u32 size; /* cdata size */
323 enum sof_ipc_ctrl_cmd cmd;
324 u32 *volume_table; /* volume table computed from tlv data*/
326 struct list_head list; /* list in sdev control list */
329 /* ASoC SOF DAPM widget */
330 struct snd_sof_widget {
331 struct snd_sof_dev *sdev;
337 struct snd_soc_dapm_widget *widget;
338 struct list_head list; /* list in sdev widget list */
340 void *private; /* core does not touch this */
343 /* ASoC SOF DAPM route */
344 struct snd_sof_route {
345 struct snd_sof_dev *sdev;
347 struct snd_soc_dapm_route *route;
348 struct list_head list; /* list in sdev route list */
353 /* ASoC DAI device */
355 struct snd_sof_dev *sdev;
357 const char *cpu_dai_name;
359 struct sof_ipc_comp_dai comp_dai;
360 struct sof_ipc_dai_config *dai_config;
361 struct list_head list; /* list in sdev dai list */
369 spinlock_t ipc_lock; /* lock for IPC users */
370 spinlock_t hw_lock; /* lock for HW IO access */
373 * ASoC components. plat_drv fields are set dynamically so
376 struct snd_soc_component_driver plat_drv;
378 /* DSP firmware boot */
379 wait_queue_head_t boot_wait;
383 /* work queue in case the probe is implemented in two steps */
384 struct work_struct probe_work;
386 /* DSP HW differentiation */
387 struct snd_sof_pdata *pdata;
390 struct snd_sof_ipc *ipc;
391 struct snd_sof_mailbox dsp_box; /* DSP initiated IPC */
392 struct snd_sof_mailbox host_box; /* Host initiated IPC */
393 struct snd_sof_mailbox stream_box; /* Stream position update */
394 struct snd_sof_ipc_msg *msg;
396 u32 next_comp_id; /* monotonic - reset during S3 */
398 /* memory bases for mmaped DSPs - set by dsp_init() */
399 void __iomem *bar[SND_SOF_BARS]; /* DSP base address */
402 size_t dsp_oops_offset;
405 struct dentry *debugfs_root;
406 struct list_head dfsentry_list;
408 /* firmware loader */
409 struct snd_dma_buffer dmab;
410 struct snd_dma_buffer dmab_bdl;
411 struct sof_ipc_fw_ready fw_ready;
412 struct sof_ipc_fw_version fw_version;
415 struct snd_soc_tplg_ops *tplg_ops;
416 struct list_head pcm_list;
417 struct list_head kcontrol_list;
418 struct list_head widget_list;
419 struct list_head dai_list;
420 struct list_head route_list;
421 struct snd_soc_component *component;
422 u32 enabled_cores_mask; /* keep track of enabled cores */
424 /* FW configuration */
425 struct sof_ipc_dma_buffer_data *info_buffer;
426 struct sof_ipc_window *info_window;
428 /* IPC timeouts in ms */
432 /* Wait queue for code loading */
433 wait_queue_head_t waitq;
437 struct snd_dma_buffer dmatb;
438 struct snd_dma_buffer dmatp;
440 wait_queue_head_t trace_sleep;
442 u32 dtrace_is_supported; /* set with Kconfig or module parameter */
443 u32 dtrace_is_enabled;
449 void *private; /* core does not touch this */
456 int snd_sof_device_probe(struct device *dev, struct snd_sof_pdata *plat_data);
457 int snd_sof_device_remove(struct device *dev);
459 int snd_sof_runtime_suspend(struct device *dev);
460 int snd_sof_runtime_resume(struct device *dev);
461 int snd_sof_runtime_idle(struct device *dev);
462 int snd_sof_resume(struct device *dev);
463 int snd_sof_suspend(struct device *dev);
465 void snd_sof_new_platform_drv(struct snd_sof_dev *sdev);
467 int snd_sof_create_page_table(struct snd_sof_dev *sdev,
468 struct snd_dma_buffer *dmab,
469 unsigned char *page_table, size_t size);
474 int snd_sof_load_firmware(struct snd_sof_dev *sdev);
475 int snd_sof_load_firmware_raw(struct snd_sof_dev *sdev);
476 int snd_sof_load_firmware_memcpy(struct snd_sof_dev *sdev);
477 int snd_sof_run_firmware(struct snd_sof_dev *sdev);
478 int snd_sof_parse_module_memcpy(struct snd_sof_dev *sdev,
479 struct snd_sof_mod_hdr *module);
480 void snd_sof_fw_unload(struct snd_sof_dev *sdev);
481 int snd_sof_fw_parse_ext_data(struct snd_sof_dev *sdev, u32 bar, u32 offset);
484 * IPC low level APIs.
486 struct snd_sof_ipc *snd_sof_ipc_init(struct snd_sof_dev *sdev);
487 void snd_sof_ipc_free(struct snd_sof_dev *sdev);
488 int snd_sof_ipc_reply(struct snd_sof_dev *sdev, u32 msg_id);
489 void snd_sof_ipc_msgs_rx(struct snd_sof_dev *sdev);
490 int snd_sof_ipc_stream_pcm_params(struct snd_sof_dev *sdev,
491 struct sof_ipc_pcm_params *params);
492 int snd_sof_dsp_mailbox_init(struct snd_sof_dev *sdev, u32 dspbox,
493 size_t dspbox_size, u32 hostbox,
494 size_t hostbox_size);
495 int snd_sof_ipc_valid(struct snd_sof_dev *sdev);
496 int sof_ipc_tx_message(struct snd_sof_ipc *ipc, u32 header,
497 void *msg_data, size_t msg_bytes, void *reply_data,
499 struct snd_sof_widget *snd_sof_find_swidget(struct snd_sof_dev *sdev,
501 struct snd_sof_widget *snd_sof_find_swidget_sname(struct snd_sof_dev *sdev,
502 const char *pcm_name,
504 struct snd_sof_dai *snd_sof_find_dai(struct snd_sof_dev *sdev,
508 struct snd_sof_pcm *snd_sof_find_spcm_dai(struct snd_sof_dev *sdev,
509 struct snd_soc_pcm_runtime *rtd)
511 struct snd_sof_pcm *spcm = NULL;
513 list_for_each_entry(spcm, &sdev->pcm_list, list) {
514 if (le32_to_cpu(spcm->pcm.dai_id) == rtd->dai_link->id)
521 struct snd_sof_pcm *snd_sof_find_spcm_name(struct snd_sof_dev *sdev,
523 struct snd_sof_pcm *snd_sof_find_spcm_comp(struct snd_sof_dev *sdev,
524 unsigned int comp_id,
526 struct snd_sof_pcm *snd_sof_find_spcm_pcm_id(struct snd_sof_dev *sdev,
527 unsigned int pcm_id);
528 void snd_sof_pcm_period_elapsed(struct snd_pcm_substream *substream);
533 int snd_sof_ipc_stream_posn(struct snd_sof_dev *sdev,
534 struct snd_sof_pcm *spcm, int direction,
535 struct sof_ipc_stream_posn *posn);
540 int snd_sof_ipc_set_get_comp_data(struct snd_sof_ipc *ipc,
541 struct snd_sof_control *scontrol, u32 ipc_cmd,
542 enum sof_ipc_ctrl_type ctrl_type,
543 enum sof_ipc_ctrl_cmd ctrl_cmd,
548 * There is no snd_sof_free_topology since topology components will
549 * be freed by snd_soc_unregister_component,
551 int snd_sof_init_topology(struct snd_sof_dev *sdev,
552 struct snd_soc_tplg_ops *ops);
553 int snd_sof_load_topology(struct snd_sof_dev *sdev, const char *file);
554 int snd_sof_complete_pipeline(struct snd_sof_dev *sdev,
555 struct snd_sof_widget *swidget);
557 int sof_load_pipeline_ipc(struct snd_sof_dev *sdev,
558 struct sof_ipc_pipe_new *pipeline,
559 struct sof_ipc_comp_reply *r);
564 int snd_sof_init_trace(struct snd_sof_dev *sdev);
565 void snd_sof_release_trace(struct snd_sof_dev *sdev);
566 void snd_sof_free_trace(struct snd_sof_dev *sdev);
567 int snd_sof_dbg_init(struct snd_sof_dev *sdev);
568 void snd_sof_free_debug(struct snd_sof_dev *sdev);
569 int snd_sof_debugfs_io_item(struct snd_sof_dev *sdev,
570 void __iomem *base, size_t size,
572 enum sof_debugfs_access_type access_type);
573 int snd_sof_debugfs_buf_item(struct snd_sof_dev *sdev,
574 void *base, size_t size,
575 const char *name, mode_t mode);
576 int snd_sof_trace_update_pos(struct snd_sof_dev *sdev,
577 struct sof_ipc_dma_trace_posn *posn);
578 void snd_sof_trace_notify_for_error(struct snd_sof_dev *sdev);
579 void snd_sof_get_status(struct snd_sof_dev *sdev, u32 panic_code,
580 u32 tracep_code, void *oops,
581 struct sof_ipc_panic_info *panic_info,
582 void *stack, size_t stack_words);
583 int snd_sof_init_trace_ipc(struct snd_sof_dev *sdev);
584 void snd_sof_handle_fw_exception(struct snd_sof_dev *sdev);
587 * Platform specific ops.
589 extern struct snd_compr_ops sof_compressed_ops;
595 int snd_sof_volume_get(struct snd_kcontrol *kcontrol,
596 struct snd_ctl_elem_value *ucontrol);
597 int snd_sof_volume_put(struct snd_kcontrol *kcontrol,
598 struct snd_ctl_elem_value *ucontrol);
599 int snd_sof_switch_get(struct snd_kcontrol *kcontrol,
600 struct snd_ctl_elem_value *ucontrol);
601 int snd_sof_switch_put(struct snd_kcontrol *kcontrol,
602 struct snd_ctl_elem_value *ucontrol);
603 int snd_sof_enum_get(struct snd_kcontrol *kcontrol,
604 struct snd_ctl_elem_value *ucontrol);
605 int snd_sof_enum_put(struct snd_kcontrol *kcontrol,
606 struct snd_ctl_elem_value *ucontrol);
607 int snd_sof_bytes_get(struct snd_kcontrol *kcontrol,
608 struct snd_ctl_elem_value *ucontrol);
609 int snd_sof_bytes_put(struct snd_kcontrol *kcontrol,
610 struct snd_ctl_elem_value *ucontrol);
611 int snd_sof_bytes_ext_put(struct snd_kcontrol *kcontrol,
612 const unsigned int __user *binary_data,
614 int snd_sof_bytes_ext_get(struct snd_kcontrol *kcontrol,
615 unsigned int __user *binary_data,
621 static inline void sof_stack(struct snd_sof_dev *sdev, void *oops, u32 *stack,
624 if (sof_arch_ops(sdev)->dsp_stack)
625 sof_arch_ops(sdev)->dsp_stack(sdev, oops, stack, stack_words);
628 static inline void sof_oops(struct snd_sof_dev *sdev, void *oops)
630 if (sof_arch_ops(sdev)->dsp_oops)
631 sof_arch_ops(sdev)->dsp_oops(sdev, oops);
634 extern const struct sof_arch_ops sof_xtensa_arch_ops;
639 void sof_io_write(struct snd_sof_dev *sdev, void __iomem *addr, u32 value);
640 void sof_io_write64(struct snd_sof_dev *sdev, void __iomem *addr, u64 value);
641 u32 sof_io_read(struct snd_sof_dev *sdev, void __iomem *addr);
642 u64 sof_io_read64(struct snd_sof_dev *sdev, void __iomem *addr);
643 void sof_mailbox_write(struct snd_sof_dev *sdev, u32 offset,
644 void *message, size_t bytes);
645 void sof_mailbox_read(struct snd_sof_dev *sdev, u32 offset,
646 void *message, size_t bytes);
647 void sof_block_write(struct snd_sof_dev *sdev, u32 bar, u32 offset, void *src,
649 void sof_block_read(struct snd_sof_dev *sdev, u32 bar, u32 offset, void *dest,
652 int sof_fw_ready(struct snd_sof_dev *sdev, u32 msg_id);
654 void intel_ipc_msg_data(struct snd_sof_dev *sdev,
655 struct snd_pcm_substream *substream,
657 int intel_ipc_pcm_params(struct snd_sof_dev *sdev,
658 struct snd_pcm_substream *substream,
659 const struct sof_ipc_pcm_params_reply *reply);
661 int intel_pcm_open(struct snd_sof_dev *sdev,
662 struct snd_pcm_substream *substream);
663 int intel_pcm_close(struct snd_sof_dev *sdev,
664 struct snd_pcm_substream *substream);