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)
37 /* global debug state set by SOF_DBG_ flags */
38 extern int sof_core_debug;
40 /* max BARs mmaped devices can use */
41 #define SND_SOF_BARS 8
43 /* time in ms for runtime suspend delay */
44 #define SND_SOF_SUSPEND_DELAY_MS 2000
46 /* DMA buffer size for trace */
47 #define DMA_BUF_SIZE_FOR_TRACE (PAGE_SIZE * 16)
49 /* max number of FE PCMs before BEs */
50 #define SOF_BE_PCM_BASE 16
52 #define SOF_IPC_DSP_REPLY 0
53 #define SOF_IPC_HOST_REPLY 1
55 /* convenience constructor for DAI driver streams */
56 #define SOF_DAI_STREAM(sname, scmin, scmax, srates, sfmt) \
57 {.stream_name = sname, .channels_min = scmin, .channels_max = scmax, \
58 .rates = srates, .formats = sfmt}
60 #define SOF_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE | \
61 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_FLOAT)
63 #define ENABLE_DEBUGFS_CACHEBUF \
64 (IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE) || \
65 IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_IPC_FLOOD_TEST))
67 #define DMA_CHAN_INVALID 0xFFFFFFFF
70 struct snd_sof_ipc_msg;
72 struct snd_sof_debugfs_map;
73 struct snd_soc_tplg_ops;
74 struct snd_soc_component;
78 * SOF DSP HW abstraction operations.
79 * Used to abstract DSP HW architecture and any IO busses between host CPU
82 struct snd_sof_dsp_ops {
84 /* probe and remove */
85 int (*probe)(struct snd_sof_dev *sof_dev); /* mandatory */
86 int (*remove)(struct snd_sof_dev *sof_dev); /* optional */
88 /* DSP core boot / reset */
89 int (*run)(struct snd_sof_dev *sof_dev); /* mandatory */
90 int (*stall)(struct snd_sof_dev *sof_dev); /* optional */
91 int (*reset)(struct snd_sof_dev *sof_dev); /* optional */
92 int (*core_power_up)(struct snd_sof_dev *sof_dev,
93 unsigned int core_mask); /* optional */
94 int (*core_power_down)(struct snd_sof_dev *sof_dev,
95 unsigned int core_mask); /* optional */
98 * Register IO: only used by respective drivers themselves,
99 * TODO: consider removing these operations and calling respective
100 * implementations directly
102 void (*write)(struct snd_sof_dev *sof_dev, void __iomem *addr,
103 u32 value); /* optional */
104 u32 (*read)(struct snd_sof_dev *sof_dev,
105 void __iomem *addr); /* optional */
106 void (*write64)(struct snd_sof_dev *sof_dev, void __iomem *addr,
107 u64 value); /* optional */
108 u64 (*read64)(struct snd_sof_dev *sof_dev,
109 void __iomem *addr); /* optional */
112 void (*block_read)(struct snd_sof_dev *sof_dev, u32 bar,
113 u32 offset, void *dest,
114 size_t size); /* mandatory */
115 void (*block_write)(struct snd_sof_dev *sof_dev, u32 bar,
116 u32 offset, void *src,
117 size_t size); /* mandatory */
120 irqreturn_t (*irq_handler)(int irq, void *context); /* optional */
121 irqreturn_t (*irq_thread)(int irq, void *context); /* optional */
124 int (*send_msg)(struct snd_sof_dev *sof_dev,
125 struct snd_sof_ipc_msg *msg); /* mandatory */
128 int (*load_firmware)(struct snd_sof_dev *sof_dev); /* mandatory */
129 int (*load_module)(struct snd_sof_dev *sof_dev,
130 struct snd_sof_mod_hdr *hdr); /* optional */
132 * FW ready checks for ABI compatibility and creates
133 * memory windows at first boot
135 int (*fw_ready)(struct snd_sof_dev *sdev, u32 msg_id); /* optional */
137 /* connect pcm substream to a host stream */
138 int (*pcm_open)(struct snd_sof_dev *sdev,
139 struct snd_pcm_substream *substream); /* optional */
140 /* disconnect pcm substream to a host stream */
141 int (*pcm_close)(struct snd_sof_dev *sdev,
142 struct snd_pcm_substream *substream); /* optional */
144 /* host stream hw params */
145 int (*pcm_hw_params)(struct snd_sof_dev *sdev,
146 struct snd_pcm_substream *substream,
147 struct snd_pcm_hw_params *params,
148 struct sof_ipc_stream_params *ipc_params); /* optional */
150 /* host stream hw_free */
151 int (*pcm_hw_free)(struct snd_sof_dev *sdev,
152 struct snd_pcm_substream *substream); /* optional */
154 /* host stream trigger */
155 int (*pcm_trigger)(struct snd_sof_dev *sdev,
156 struct snd_pcm_substream *substream,
157 int cmd); /* optional */
159 /* host stream pointer */
160 snd_pcm_uframes_t (*pcm_pointer)(struct snd_sof_dev *sdev,
161 struct snd_pcm_substream *substream); /* optional */
163 /* host read DSP stream data */
164 void (*ipc_msg_data)(struct snd_sof_dev *sdev,
165 struct snd_pcm_substream *substream,
166 void *p, size_t sz); /* mandatory */
168 /* host configure DSP HW parameters */
169 int (*ipc_pcm_params)(struct snd_sof_dev *sdev,
170 struct snd_pcm_substream *substream,
171 const struct sof_ipc_pcm_params_reply *reply); /* mandatory */
173 /* pre/post firmware run */
174 int (*pre_fw_run)(struct snd_sof_dev *sof_dev); /* optional */
175 int (*post_fw_run)(struct snd_sof_dev *sof_dev); /* optional */
178 int (*suspend)(struct snd_sof_dev *sof_dev); /* optional */
179 int (*resume)(struct snd_sof_dev *sof_dev); /* optional */
180 int (*runtime_suspend)(struct snd_sof_dev *sof_dev); /* optional */
181 int (*runtime_resume)(struct snd_sof_dev *sof_dev); /* optional */
182 int (*runtime_idle)(struct snd_sof_dev *sof_dev); /* optional */
183 int (*set_hw_params_upon_resume)(struct snd_sof_dev *sdev); /* optional */
186 int (*set_clk)(struct snd_sof_dev *sof_dev, u32 freq); /* optional */
189 const struct snd_sof_debugfs_map *debug_map; /* optional */
190 int debug_map_count; /* optional */
191 void (*dbg_dump)(struct snd_sof_dev *sof_dev,
192 u32 flags); /* optional */
193 void (*ipc_dump)(struct snd_sof_dev *sof_dev); /* optional */
195 /* host DMA trace initialization */
196 int (*trace_init)(struct snd_sof_dev *sdev,
197 u32 *stream_tag); /* optional */
198 int (*trace_release)(struct snd_sof_dev *sdev); /* optional */
199 int (*trace_trigger)(struct snd_sof_dev *sdev,
200 int cmd); /* optional */
203 int (*get_bar_index)(struct snd_sof_dev *sdev,
204 u32 type); /* optional */
205 int (*get_mailbox_offset)(struct snd_sof_dev *sdev);/* mandatory for common loader code */
206 int (*get_window_offset)(struct snd_sof_dev *sdev,
207 u32 id);/* mandatory for common loader code */
210 struct snd_soc_dai_driver *drv;
214 /* DSP architecture specific callbacks for oops and stack dumps */
215 struct sof_arch_ops {
216 void (*dsp_oops)(struct snd_sof_dev *sdev, void *oops);
217 void (*dsp_stack)(struct snd_sof_dev *sdev, void *oops,
218 u32 *stack, u32 stack_words);
221 #define sof_arch_ops(sdev) ((sdev)->pdata->desc->arch_ops)
223 /* DSP device HW descriptor mapping between bus ID and ops */
224 struct sof_ops_table {
225 const struct sof_dev_desc *desc;
226 const struct snd_sof_dsp_ops *ops;
229 enum sof_dfsentry_type {
230 SOF_DFSENTRY_TYPE_IOMEM = 0,
231 SOF_DFSENTRY_TYPE_BUF,
234 enum sof_debugfs_access_type {
235 SOF_DEBUGFS_ACCESS_ALWAYS = 0,
236 SOF_DEBUGFS_ACCESS_D0_ONLY,
239 /* FS entry for debug files that can expose DSP memories, registers */
240 struct snd_sof_dfsentry {
242 enum sof_dfsentry_type type;
244 * access_type specifies if the
245 * memory -> DSP resource (memory, register etc) is always accessible
246 * or if it is accessible only when the DSP is in D0.
248 enum sof_debugfs_access_type access_type;
249 #if ENABLE_DEBUGFS_CACHEBUF
250 char *cache_buf; /* buffer to cache the contents of debugfs memory */
252 struct snd_sof_dev *sdev;
253 struct list_head list; /* list in sdev dfsentry list */
255 void __iomem *io_mem;
260 /* Debug mapping for any DSP memory or registers that can used for debug */
261 struct snd_sof_debugfs_map {
267 * access_type specifies if the memory is always accessible
268 * or if it is accessible only when the DSP is in D0.
270 enum sof_debugfs_access_type access_type;
273 /* mailbox descriptor, used for host <-> DSP IPC */
274 struct snd_sof_mailbox {
279 /* IPC message descriptor for host <-> DSP IO */
280 struct snd_sof_ipc_msg {
289 wait_queue_head_t waitq;
293 /* PCM stream, mapped to FW component */
294 struct snd_sof_pcm_stream {
296 struct snd_dma_buffer page_table;
297 struct sof_ipc_stream_posn posn;
298 struct snd_pcm_substream *substream;
299 struct work_struct period_elapsed_work;
302 /* ALSA SOF PCM device */
304 struct snd_sof_dev *sdev;
305 struct snd_soc_tplg_pcm pcm;
306 struct snd_sof_pcm_stream stream[2];
307 struct list_head list; /* list in sdev pcm list */
308 struct snd_pcm_hw_params params[2];
309 bool prepared[2]; /* PCM_PARAMS set successfully */
312 /* ALSA SOF Kcontrol device */
313 struct snd_sof_control {
314 struct snd_sof_dev *sdev;
316 int min_volume_step; /* min volume step for volume_table */
317 int max_volume_step; /* max volume step for volume_table */
319 u32 readback_offset; /* offset to mmaped data if used */
320 struct sof_ipc_ctrl_data *control_data;
321 u32 size; /* cdata size */
322 enum sof_ipc_ctrl_cmd cmd;
323 u32 *volume_table; /* volume table computed from tlv data*/
325 struct list_head list; /* list in sdev control list */
328 /* ASoC SOF DAPM widget */
329 struct snd_sof_widget {
330 struct snd_sof_dev *sdev;
336 struct snd_soc_dapm_widget *widget;
337 struct list_head list; /* list in sdev widget list */
339 void *private; /* core does not touch this */
342 /* ASoC SOF DAPM route */
343 struct snd_sof_route {
344 struct snd_sof_dev *sdev;
346 struct snd_soc_dapm_route *route;
347 struct list_head list; /* list in sdev route list */
352 /* ASoC DAI device */
354 struct snd_sof_dev *sdev;
356 const char *cpu_dai_name;
358 struct sof_ipc_comp_dai comp_dai;
359 struct sof_ipc_dai_config *dai_config;
360 struct list_head list; /* list in sdev dai list */
368 spinlock_t ipc_lock; /* lock for IPC users */
369 spinlock_t hw_lock; /* lock for HW IO access */
372 * ASoC components. plat_drv fields are set dynamically so
375 struct snd_soc_component_driver plat_drv;
377 /* DSP firmware boot */
378 wait_queue_head_t boot_wait;
382 /* work queue in case the probe is implemented in two steps */
383 struct work_struct probe_work;
385 /* DSP HW differentiation */
386 struct snd_sof_pdata *pdata;
389 struct snd_sof_ipc *ipc;
390 struct snd_sof_mailbox dsp_box; /* DSP initiated IPC */
391 struct snd_sof_mailbox host_box; /* Host initiated IPC */
392 struct snd_sof_mailbox stream_box; /* Stream position update */
393 struct snd_sof_ipc_msg *msg;
395 u32 next_comp_id; /* monotonic - reset during S3 */
397 /* memory bases for mmaped DSPs - set by dsp_init() */
398 void __iomem *bar[SND_SOF_BARS]; /* DSP base address */
401 size_t dsp_oops_offset;
404 struct dentry *debugfs_root;
405 struct list_head dfsentry_list;
407 /* firmware loader */
408 struct snd_dma_buffer dmab;
409 struct snd_dma_buffer dmab_bdl;
410 struct sof_ipc_fw_ready fw_ready;
411 struct sof_ipc_fw_version fw_version;
414 struct snd_soc_tplg_ops *tplg_ops;
415 struct list_head pcm_list;
416 struct list_head kcontrol_list;
417 struct list_head widget_list;
418 struct list_head dai_list;
419 struct list_head route_list;
420 struct snd_soc_component *component;
421 u32 enabled_cores_mask; /* keep track of enabled cores */
423 /* FW configuration */
424 struct sof_ipc_dma_buffer_data *info_buffer;
425 struct sof_ipc_window *info_window;
427 /* IPC timeouts in ms */
431 /* Wait queue for code loading */
432 wait_queue_head_t waitq;
436 struct snd_dma_buffer dmatb;
437 struct snd_dma_buffer dmatp;
439 wait_queue_head_t trace_sleep;
441 u32 dtrace_is_supported; /* set with Kconfig or module parameter */
442 u32 dtrace_is_enabled;
448 void *private; /* core does not touch this */
455 int snd_sof_device_probe(struct device *dev, struct snd_sof_pdata *plat_data);
456 int snd_sof_device_remove(struct device *dev);
458 int snd_sof_runtime_suspend(struct device *dev);
459 int snd_sof_runtime_resume(struct device *dev);
460 int snd_sof_runtime_idle(struct device *dev);
461 int snd_sof_resume(struct device *dev);
462 int snd_sof_suspend(struct device *dev);
464 void snd_sof_new_platform_drv(struct snd_sof_dev *sdev);
466 int snd_sof_create_page_table(struct snd_sof_dev *sdev,
467 struct snd_dma_buffer *dmab,
468 unsigned char *page_table, size_t size);
473 int snd_sof_load_firmware(struct snd_sof_dev *sdev);
474 int snd_sof_load_firmware_raw(struct snd_sof_dev *sdev);
475 int snd_sof_load_firmware_memcpy(struct snd_sof_dev *sdev);
476 int snd_sof_run_firmware(struct snd_sof_dev *sdev);
477 int snd_sof_parse_module_memcpy(struct snd_sof_dev *sdev,
478 struct snd_sof_mod_hdr *module);
479 void snd_sof_fw_unload(struct snd_sof_dev *sdev);
480 int snd_sof_fw_parse_ext_data(struct snd_sof_dev *sdev, u32 bar, u32 offset);
483 * IPC low level APIs.
485 struct snd_sof_ipc *snd_sof_ipc_init(struct snd_sof_dev *sdev);
486 void snd_sof_ipc_free(struct snd_sof_dev *sdev);
487 int snd_sof_ipc_reply(struct snd_sof_dev *sdev, u32 msg_id);
488 void snd_sof_ipc_msgs_rx(struct snd_sof_dev *sdev);
489 int snd_sof_ipc_stream_pcm_params(struct snd_sof_dev *sdev,
490 struct sof_ipc_pcm_params *params);
491 int snd_sof_dsp_mailbox_init(struct snd_sof_dev *sdev, u32 dspbox,
492 size_t dspbox_size, u32 hostbox,
493 size_t hostbox_size);
494 int snd_sof_ipc_valid(struct snd_sof_dev *sdev);
495 int sof_ipc_tx_message(struct snd_sof_ipc *ipc, u32 header,
496 void *msg_data, size_t msg_bytes, void *reply_data,
498 struct snd_sof_widget *snd_sof_find_swidget(struct snd_sof_dev *sdev,
500 struct snd_sof_widget *snd_sof_find_swidget_sname(struct snd_sof_dev *sdev,
501 const char *pcm_name,
503 struct snd_sof_dai *snd_sof_find_dai(struct snd_sof_dev *sdev,
507 struct snd_sof_pcm *snd_sof_find_spcm_dai(struct snd_sof_dev *sdev,
508 struct snd_soc_pcm_runtime *rtd)
510 struct snd_sof_pcm *spcm = NULL;
512 list_for_each_entry(spcm, &sdev->pcm_list, list) {
513 if (le32_to_cpu(spcm->pcm.dai_id) == rtd->dai_link->id)
520 struct snd_sof_pcm *snd_sof_find_spcm_name(struct snd_sof_dev *sdev,
522 struct snd_sof_pcm *snd_sof_find_spcm_comp(struct snd_sof_dev *sdev,
523 unsigned int comp_id,
525 struct snd_sof_pcm *snd_sof_find_spcm_pcm_id(struct snd_sof_dev *sdev,
526 unsigned int pcm_id);
527 void snd_sof_pcm_period_elapsed(struct snd_pcm_substream *substream);
532 int snd_sof_ipc_stream_posn(struct snd_sof_dev *sdev,
533 struct snd_sof_pcm *spcm, int direction,
534 struct sof_ipc_stream_posn *posn);
539 int snd_sof_ipc_set_get_comp_data(struct snd_sof_ipc *ipc,
540 struct snd_sof_control *scontrol, u32 ipc_cmd,
541 enum sof_ipc_ctrl_type ctrl_type,
542 enum sof_ipc_ctrl_cmd ctrl_cmd,
547 * There is no snd_sof_free_topology since topology components will
548 * be freed by snd_soc_unregister_component,
550 int snd_sof_init_topology(struct snd_sof_dev *sdev,
551 struct snd_soc_tplg_ops *ops);
552 int snd_sof_load_topology(struct snd_sof_dev *sdev, const char *file);
553 int snd_sof_complete_pipeline(struct snd_sof_dev *sdev,
554 struct snd_sof_widget *swidget);
556 int sof_load_pipeline_ipc(struct snd_sof_dev *sdev,
557 struct sof_ipc_pipe_new *pipeline,
558 struct sof_ipc_comp_reply *r);
563 int snd_sof_init_trace(struct snd_sof_dev *sdev);
564 void snd_sof_release_trace(struct snd_sof_dev *sdev);
565 void snd_sof_free_trace(struct snd_sof_dev *sdev);
566 int snd_sof_dbg_init(struct snd_sof_dev *sdev);
567 void snd_sof_free_debug(struct snd_sof_dev *sdev);
568 int snd_sof_debugfs_io_item(struct snd_sof_dev *sdev,
569 void __iomem *base, size_t size,
571 enum sof_debugfs_access_type access_type);
572 int snd_sof_debugfs_buf_item(struct snd_sof_dev *sdev,
573 void *base, size_t size,
574 const char *name, mode_t mode);
575 int snd_sof_trace_update_pos(struct snd_sof_dev *sdev,
576 struct sof_ipc_dma_trace_posn *posn);
577 void snd_sof_trace_notify_for_error(struct snd_sof_dev *sdev);
578 void snd_sof_get_status(struct snd_sof_dev *sdev, u32 panic_code,
579 u32 tracep_code, void *oops,
580 struct sof_ipc_panic_info *panic_info,
581 void *stack, size_t stack_words);
582 int snd_sof_init_trace_ipc(struct snd_sof_dev *sdev);
585 * Platform specific ops.
587 extern struct snd_compr_ops sof_compressed_ops;
593 int snd_sof_volume_get(struct snd_kcontrol *kcontrol,
594 struct snd_ctl_elem_value *ucontrol);
595 int snd_sof_volume_put(struct snd_kcontrol *kcontrol,
596 struct snd_ctl_elem_value *ucontrol);
597 int snd_sof_switch_get(struct snd_kcontrol *kcontrol,
598 struct snd_ctl_elem_value *ucontrol);
599 int snd_sof_switch_put(struct snd_kcontrol *kcontrol,
600 struct snd_ctl_elem_value *ucontrol);
601 int snd_sof_enum_get(struct snd_kcontrol *kcontrol,
602 struct snd_ctl_elem_value *ucontrol);
603 int snd_sof_enum_put(struct snd_kcontrol *kcontrol,
604 struct snd_ctl_elem_value *ucontrol);
605 int snd_sof_bytes_get(struct snd_kcontrol *kcontrol,
606 struct snd_ctl_elem_value *ucontrol);
607 int snd_sof_bytes_put(struct snd_kcontrol *kcontrol,
608 struct snd_ctl_elem_value *ucontrol);
609 int snd_sof_bytes_ext_put(struct snd_kcontrol *kcontrol,
610 const unsigned int __user *binary_data,
612 int snd_sof_bytes_ext_get(struct snd_kcontrol *kcontrol,
613 unsigned int __user *binary_data,
619 static inline void sof_stack(struct snd_sof_dev *sdev, void *oops, u32 *stack,
622 if (sof_arch_ops(sdev)->dsp_stack)
623 sof_arch_ops(sdev)->dsp_stack(sdev, oops, stack, stack_words);
626 static inline void sof_oops(struct snd_sof_dev *sdev, void *oops)
628 if (sof_arch_ops(sdev)->dsp_oops)
629 sof_arch_ops(sdev)->dsp_oops(sdev, oops);
632 extern const struct sof_arch_ops sof_xtensa_arch_ops;
637 void sof_io_write(struct snd_sof_dev *sdev, void __iomem *addr, u32 value);
638 void sof_io_write64(struct snd_sof_dev *sdev, void __iomem *addr, u64 value);
639 u32 sof_io_read(struct snd_sof_dev *sdev, void __iomem *addr);
640 u64 sof_io_read64(struct snd_sof_dev *sdev, void __iomem *addr);
641 void sof_mailbox_write(struct snd_sof_dev *sdev, u32 offset,
642 void *message, size_t bytes);
643 void sof_mailbox_read(struct snd_sof_dev *sdev, u32 offset,
644 void *message, size_t bytes);
645 void sof_block_write(struct snd_sof_dev *sdev, u32 bar, u32 offset, void *src,
647 void sof_block_read(struct snd_sof_dev *sdev, u32 bar, u32 offset, void *dest,
650 int sof_fw_ready(struct snd_sof_dev *sdev, u32 msg_id);
652 void intel_ipc_msg_data(struct snd_sof_dev *sdev,
653 struct snd_pcm_substream *substream,
655 int intel_ipc_pcm_params(struct snd_sof_dev *sdev,
656 struct snd_pcm_substream *substream,
657 const struct sof_ipc_pcm_params_reply *reply);
659 int intel_pcm_open(struct snd_sof_dev *sdev,
660 struct snd_pcm_substream *substream);
661 int intel_pcm_close(struct snd_sof_dev *sdev,
662 struct snd_pcm_substream *substream);