ASoC: SOF: Intel: clarify Copyright information
[linux-2.6-block.git] / sound / soc / sof / sof-priv.h
1 /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) */
2 /*
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.
5  *
6  * Copyright(c) 2018 Intel Corporation
7  *
8  * Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9  */
10
11 #ifndef __SOUND_SOC_SOF_PRIV_H
12 #define __SOUND_SOC_SOF_PRIV_H
13
14 #include <linux/device.h>
15 #include <sound/hdaudio.h>
16 #include <sound/sof.h>
17 #include <sound/sof/info.h>
18 #include <sound/sof/pm.h>
19 #include <sound/sof/trace.h>
20 #include <uapi/sound/sof/fw.h>
21 #include <sound/sof/ext_manifest.h>
22
23 struct snd_sof_pcm_stream;
24
25 /* Flag definitions used in sof_core_debug (sof_debug module parameter) */
26 #define SOF_DBG_ENABLE_TRACE    BIT(0)
27 #define SOF_DBG_RETAIN_CTX      BIT(1)  /* prevent DSP D3 on FW exception */
28 #define SOF_DBG_VERIFY_TPLG     BIT(2) /* verify topology during load */
29 #define SOF_DBG_DYNAMIC_PIPELINES_OVERRIDE      BIT(3) /* 0: use topology token
30                                                         * 1: override topology
31                                                         */
32 #define SOF_DBG_DYNAMIC_PIPELINES_ENABLE        BIT(4) /* 0: use static pipelines
33                                                         * 1: use dynamic pipelines
34                                                         */
35 #define SOF_DBG_DISABLE_MULTICORE               BIT(5) /* schedule all pipelines/widgets
36                                                         * on primary core
37                                                         */
38 #define SOF_DBG_PRINT_ALL_DUMPS         BIT(6) /* Print all ipc and dsp dumps */
39 #define SOF_DBG_IGNORE_D3_PERSISTENT            BIT(7) /* ignore the DSP D3 persistent capability
40                                                         * and always download firmware upon D3 exit
41                                                         */
42 #define SOF_DBG_PRINT_DMA_POSITION_UPDATE_LOGS  BIT(8) /* print DMA position updates
43                                                         * in dmesg logs
44                                                         */
45 #define SOF_DBG_PRINT_IPC_SUCCESS_LOGS          BIT(9) /* print IPC success
46                                                         * in dmesg logs
47                                                         */
48 #define SOF_DBG_FORCE_NOCODEC                   BIT(10) /* ignore all codec-related
49                                                          * configurations
50                                                          */
51 #define SOF_DBG_DUMP_IPC_MESSAGE_PAYLOAD        BIT(11) /* On top of the IPC message header
52                                                          * dump the message payload also
53                                                          */
54 #define SOF_DBG_DSPLESS_MODE                    BIT(15) /* Do not initialize and use the DSP */
55
56 /* Flag definitions used for controlling the DSP dump behavior */
57 #define SOF_DBG_DUMP_REGS               BIT(0)
58 #define SOF_DBG_DUMP_MBOX               BIT(1)
59 #define SOF_DBG_DUMP_TEXT               BIT(2)
60 #define SOF_DBG_DUMP_PCI                BIT(3)
61 /* Output this dump (at the DEBUG level) only when SOF_DBG_PRINT_ALL_DUMPS is set */
62 #define SOF_DBG_DUMP_OPTIONAL           BIT(4)
63
64 /* global debug state set by SOF_DBG_ flags */
65 bool sof_debug_check_flag(int mask);
66
67 /* max BARs mmaped devices can use */
68 #define SND_SOF_BARS    8
69
70 /* time in ms for runtime suspend delay */
71 #define SND_SOF_SUSPEND_DELAY_MS        2000
72
73 /* DMA buffer size for trace */
74 #define DMA_BUF_SIZE_FOR_TRACE (PAGE_SIZE * 16)
75
76 #define SOF_IPC_DSP_REPLY               0
77 #define SOF_IPC_HOST_REPLY              1
78
79 /* convenience constructor for DAI driver streams */
80 #define SOF_DAI_STREAM(sname, scmin, scmax, srates, sfmt) \
81         {.stream_name = sname, .channels_min = scmin, .channels_max = scmax, \
82          .rates = srates, .formats = sfmt}
83
84 #define SOF_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE | \
85         SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_FLOAT)
86
87 /* So far the primary core on all DSPs has ID 0 */
88 #define SOF_DSP_PRIMARY_CORE 0
89
90 /* max number of DSP cores */
91 #define SOF_MAX_DSP_NUM_CORES 8
92
93 struct sof_dsp_power_state {
94         u32 state;
95         u32 substate; /* platform-specific */
96 };
97
98 /* System suspend target state */
99 enum sof_system_suspend_state {
100         SOF_SUSPEND_NONE = 0,
101         SOF_SUSPEND_S0IX,
102         SOF_SUSPEND_S3,
103         SOF_SUSPEND_S4,
104         SOF_SUSPEND_S5,
105 };
106
107 enum sof_dfsentry_type {
108         SOF_DFSENTRY_TYPE_IOMEM = 0,
109         SOF_DFSENTRY_TYPE_BUF,
110 };
111
112 enum sof_debugfs_access_type {
113         SOF_DEBUGFS_ACCESS_ALWAYS = 0,
114         SOF_DEBUGFS_ACCESS_D0_ONLY,
115 };
116
117 struct sof_compr_stream {
118         u64 copied_total;
119         u32 sampling_rate;
120         u16 channels;
121         u16 sample_container_bytes;
122         size_t posn_offset;
123 };
124
125 struct snd_sof_dev;
126 struct snd_sof_ipc_msg;
127 struct snd_sof_ipc;
128 struct snd_sof_debugfs_map;
129 struct snd_soc_tplg_ops;
130 struct snd_soc_component;
131 struct snd_sof_pdata;
132
133 /**
134  * struct snd_sof_platform_stream_params - platform dependent stream parameters
135  * @stream_tag:         Stream tag to use
136  * @use_phy_addr:       Use the provided @phy_addr for configuration
137  * @phy_addr:           Platform dependent address to be used, if  @use_phy_addr
138  *                      is true
139  * @no_ipc_position:    Disable position update IPC from firmware
140  */
141 struct snd_sof_platform_stream_params {
142         u16 stream_tag;
143         bool use_phy_address;
144         u32 phy_addr;
145         bool no_ipc_position;
146         bool cont_update_posn;
147 };
148
149 /**
150  * struct sof_firmware - Container struct for SOF firmware
151  * @fw:                 Pointer to the firmware
152  * @payload_offset:     Offset of the data within the loaded firmware image to be
153  *                      loaded to the DSP (skipping for example ext_manifest section)
154  */
155 struct sof_firmware {
156         const struct firmware *fw;
157         u32 payload_offset;
158 };
159
160 enum sof_dai_access {
161         SOF_DAI_DSP_ACCESS,     /* access from DSP only */
162         SOF_DAI_HOST_ACCESS,    /* access from host only */
163
164         SOF_DAI_ACCESS_NUM
165 };
166
167 /*
168  * SOF DSP HW abstraction operations.
169  * Used to abstract DSP HW architecture and any IO busses between host CPU
170  * and DSP device(s).
171  */
172 struct snd_sof_dsp_ops {
173
174         /* probe/remove/shutdown */
175         int (*probe_early)(struct snd_sof_dev *sof_dev); /* optional */
176         int (*probe)(struct snd_sof_dev *sof_dev); /* mandatory */
177         void (*remove)(struct snd_sof_dev *sof_dev); /* optional */
178         void (*remove_late)(struct snd_sof_dev *sof_dev); /* optional */
179         int (*shutdown)(struct snd_sof_dev *sof_dev); /* optional */
180
181         /* DSP core boot / reset */
182         int (*run)(struct snd_sof_dev *sof_dev); /* mandatory */
183         int (*stall)(struct snd_sof_dev *sof_dev, unsigned int core_mask); /* optional */
184         int (*reset)(struct snd_sof_dev *sof_dev); /* optional */
185         int (*core_get)(struct snd_sof_dev *sof_dev, int core); /* optional */
186         int (*core_put)(struct snd_sof_dev *sof_dev, int core); /* optional */
187
188         /*
189          * Register IO: only used by respective drivers themselves,
190          * TODO: consider removing these operations and calling respective
191          * implementations directly
192          */
193         void (*write8)(struct snd_sof_dev *sof_dev, void __iomem *addr,
194                        u8 value); /* optional */
195         u8 (*read8)(struct snd_sof_dev *sof_dev,
196                     void __iomem *addr); /* optional */
197         void (*write)(struct snd_sof_dev *sof_dev, void __iomem *addr,
198                       u32 value); /* optional */
199         u32 (*read)(struct snd_sof_dev *sof_dev,
200                     void __iomem *addr); /* optional */
201         void (*write64)(struct snd_sof_dev *sof_dev, void __iomem *addr,
202                         u64 value); /* optional */
203         u64 (*read64)(struct snd_sof_dev *sof_dev,
204                       void __iomem *addr); /* optional */
205
206         /* memcpy IO */
207         int (*block_read)(struct snd_sof_dev *sof_dev,
208                           enum snd_sof_fw_blk_type type, u32 offset,
209                           void *dest, size_t size); /* mandatory */
210         int (*block_write)(struct snd_sof_dev *sof_dev,
211                            enum snd_sof_fw_blk_type type, u32 offset,
212                            void *src, size_t size); /* mandatory */
213
214         /* Mailbox IO */
215         void (*mailbox_read)(struct snd_sof_dev *sof_dev,
216                              u32 offset, void *dest,
217                              size_t size); /* optional */
218         void (*mailbox_write)(struct snd_sof_dev *sof_dev,
219                               u32 offset, void *src,
220                               size_t size); /* optional */
221
222         /* doorbell */
223         irqreturn_t (*irq_handler)(int irq, void *context); /* optional */
224         irqreturn_t (*irq_thread)(int irq, void *context); /* optional */
225
226         /* ipc */
227         int (*send_msg)(struct snd_sof_dev *sof_dev,
228                         struct snd_sof_ipc_msg *msg); /* mandatory */
229
230         /* FW loading */
231         int (*load_firmware)(struct snd_sof_dev *sof_dev); /* mandatory */
232         int (*load_module)(struct snd_sof_dev *sof_dev,
233                            struct snd_sof_mod_hdr *hdr); /* optional */
234
235         /* connect pcm substream to a host stream */
236         int (*pcm_open)(struct snd_sof_dev *sdev,
237                         struct snd_pcm_substream *substream); /* optional */
238         /* disconnect pcm substream to a host stream */
239         int (*pcm_close)(struct snd_sof_dev *sdev,
240                          struct snd_pcm_substream *substream); /* optional */
241
242         /* host stream hw params */
243         int (*pcm_hw_params)(struct snd_sof_dev *sdev,
244                              struct snd_pcm_substream *substream,
245                              struct snd_pcm_hw_params *params,
246                              struct snd_sof_platform_stream_params *platform_params); /* optional */
247
248         /* host stream hw_free */
249         int (*pcm_hw_free)(struct snd_sof_dev *sdev,
250                            struct snd_pcm_substream *substream); /* optional */
251
252         /* host stream trigger */
253         int (*pcm_trigger)(struct snd_sof_dev *sdev,
254                            struct snd_pcm_substream *substream,
255                            int cmd); /* optional */
256
257         /* host stream pointer */
258         snd_pcm_uframes_t (*pcm_pointer)(struct snd_sof_dev *sdev,
259                                          struct snd_pcm_substream *substream); /* optional */
260
261         /* pcm ack */
262         int (*pcm_ack)(struct snd_sof_dev *sdev, struct snd_pcm_substream *substream); /* optional */
263
264         /*
265          * optional callback to retrieve the number of frames left/arrived from/to
266          * the DSP on the DAI side (link/codec/DMIC/etc).
267          *
268          * The callback is used when the firmware does not provide this information
269          * via the shared SRAM window and it can be retrieved by host.
270          */
271         u64 (*get_dai_frame_counter)(struct snd_sof_dev *sdev,
272                                      struct snd_soc_component *component,
273                                      struct snd_pcm_substream *substream); /* optional */
274
275         /*
276          * Optional callback to retrieve the number of bytes left/arrived from/to
277          * the DSP on the host side (bytes between host ALSA buffer and DSP).
278          *
279          * The callback is needed for ALSA delay reporting.
280          */
281         u64 (*get_host_byte_counter)(struct snd_sof_dev *sdev,
282                                      struct snd_soc_component *component,
283                                      struct snd_pcm_substream *substream); /* optional */
284
285         /* host read DSP stream data */
286         int (*ipc_msg_data)(struct snd_sof_dev *sdev,
287                             struct snd_sof_pcm_stream *sps,
288                             void *p, size_t sz); /* mandatory */
289
290         /* host side configuration of the stream's data offset in stream mailbox area */
291         int (*set_stream_data_offset)(struct snd_sof_dev *sdev,
292                                       struct snd_sof_pcm_stream *sps,
293                                       size_t posn_offset); /* optional */
294
295         /* pre/post firmware run */
296         int (*pre_fw_run)(struct snd_sof_dev *sof_dev); /* optional */
297         int (*post_fw_run)(struct snd_sof_dev *sof_dev); /* optional */
298
299         /* parse platform specific extended manifest, optional */
300         int (*parse_platform_ext_manifest)(struct snd_sof_dev *sof_dev,
301                                            const struct sof_ext_man_elem_header *hdr);
302
303         /* DSP PM */
304         int (*suspend)(struct snd_sof_dev *sof_dev,
305                        u32 target_state); /* optional */
306         int (*resume)(struct snd_sof_dev *sof_dev); /* optional */
307         int (*runtime_suspend)(struct snd_sof_dev *sof_dev); /* optional */
308         int (*runtime_resume)(struct snd_sof_dev *sof_dev); /* optional */
309         int (*runtime_idle)(struct snd_sof_dev *sof_dev); /* optional */
310         int (*set_hw_params_upon_resume)(struct snd_sof_dev *sdev); /* optional */
311         int (*set_power_state)(struct snd_sof_dev *sdev,
312                                const struct sof_dsp_power_state *target_state); /* optional */
313
314         /* DSP clocking */
315         int (*set_clk)(struct snd_sof_dev *sof_dev, u32 freq); /* optional */
316
317         /* debug */
318         const struct snd_sof_debugfs_map *debug_map; /* optional */
319         int debug_map_count; /* optional */
320         void (*dbg_dump)(struct snd_sof_dev *sof_dev,
321                          u32 flags); /* optional */
322         void (*ipc_dump)(struct snd_sof_dev *sof_dev); /* optional */
323         int (*debugfs_add_region_item)(struct snd_sof_dev *sdev,
324                                        enum snd_sof_fw_blk_type blk_type, u32 offset,
325                                        size_t size, const char *name,
326                                        enum sof_debugfs_access_type access_type); /* optional */
327
328         /* host DMA trace (IPC3) */
329         int (*trace_init)(struct snd_sof_dev *sdev,
330                           struct snd_dma_buffer *dmatb,
331                           struct sof_ipc_dma_trace_params_ext *dtrace_params); /* optional */
332         int (*trace_release)(struct snd_sof_dev *sdev); /* optional */
333         int (*trace_trigger)(struct snd_sof_dev *sdev,
334                              int cmd); /* optional */
335
336         /* misc */
337         int (*get_bar_index)(struct snd_sof_dev *sdev,
338                              u32 type); /* optional */
339         int (*get_mailbox_offset)(struct snd_sof_dev *sdev);/* mandatory for common loader code */
340         int (*get_window_offset)(struct snd_sof_dev *sdev,
341                                  u32 id);/* mandatory for common loader code */
342
343         /* machine driver ops */
344         int (*machine_register)(struct snd_sof_dev *sdev,
345                                 void *pdata); /* optional */
346         void (*machine_unregister)(struct snd_sof_dev *sdev,
347                                    void *pdata); /* optional */
348         struct snd_soc_acpi_mach * (*machine_select)(struct snd_sof_dev *sdev); /* optional */
349         void (*set_mach_params)(struct snd_soc_acpi_mach *mach,
350                                 struct snd_sof_dev *sdev); /* optional */
351
352         /* IPC client ops */
353         int (*register_ipc_clients)(struct snd_sof_dev *sdev); /* optional */
354         void (*unregister_ipc_clients)(struct snd_sof_dev *sdev); /* optional */
355
356         /* DAI ops */
357         struct snd_soc_dai_driver *drv;
358         int num_drv;
359
360         bool (*is_chain_dma_supported)(struct snd_sof_dev *sdev, u32 dai_type); /* optional */
361
362         /* ALSA HW info flags, will be stored in snd_pcm_runtime.hw.info */
363         u32 hw_info;
364
365         const struct dsp_arch_ops *dsp_arch_ops;
366 };
367
368 /* DSP architecture specific callbacks for oops and stack dumps */
369 struct dsp_arch_ops {
370         void (*dsp_oops)(struct snd_sof_dev *sdev, const char *level, void *oops);
371         void (*dsp_stack)(struct snd_sof_dev *sdev, const char *level, void *oops,
372                           u32 *stack, u32 stack_words);
373 };
374
375 #define sof_dsp_arch_ops(sdev) ((sdev)->pdata->desc->ops->dsp_arch_ops)
376
377 /* FS entry for debug files that can expose DSP memories, registers */
378 struct snd_sof_dfsentry {
379         size_t size;
380         size_t buf_data_size;  /* length of buffered data for file read operation */
381         enum sof_dfsentry_type type;
382         /*
383          * access_type specifies if the
384          * memory -> DSP resource (memory, register etc) is always accessible
385          * or if it is accessible only when the DSP is in D0.
386          */
387         enum sof_debugfs_access_type access_type;
388 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE)
389         char *cache_buf; /* buffer to cache the contents of debugfs memory */
390 #endif
391         struct snd_sof_dev *sdev;
392         struct list_head list;  /* list in sdev dfsentry list */
393         union {
394                 void __iomem *io_mem;
395                 void *buf;
396         };
397 };
398
399 /* Debug mapping for any DSP memory or registers that can used for debug */
400 struct snd_sof_debugfs_map {
401         const char *name;
402         u32 bar;
403         u32 offset;
404         u32 size;
405         /*
406          * access_type specifies if the memory is always accessible
407          * or if it is accessible only when the DSP is in D0.
408          */
409         enum sof_debugfs_access_type access_type;
410 };
411
412 /* mailbox descriptor, used for host <-> DSP IPC */
413 struct snd_sof_mailbox {
414         u32 offset;
415         size_t size;
416 };
417
418 /* IPC message descriptor for host <-> DSP IO */
419 struct snd_sof_ipc_msg {
420         /* message data */
421         void *msg_data;
422         void *reply_data;
423         size_t msg_size;
424         size_t reply_size;
425         int reply_error;
426
427         /* notification, firmware initiated messages */
428         void *rx_data;
429
430         wait_queue_head_t waitq;
431         bool ipc_complete;
432 };
433
434 /**
435  * struct sof_ipc_fw_tracing_ops - IPC-specific firmware tracing ops
436  * @init:       Function pointer for initialization of the tracing
437  * @free:       Optional function pointer for freeing of the tracing
438  * @fw_crashed: Optional function pointer to notify the tracing of a firmware crash
439  * @suspend:    Function pointer for system/runtime suspend
440  * @resume:     Function pointer for system/runtime resume
441  */
442 struct sof_ipc_fw_tracing_ops {
443         int (*init)(struct snd_sof_dev *sdev);
444         void (*free)(struct snd_sof_dev *sdev);
445         void (*fw_crashed)(struct snd_sof_dev *sdev);
446         void (*suspend)(struct snd_sof_dev *sdev, pm_message_t pm_state);
447         int (*resume)(struct snd_sof_dev *sdev);
448 };
449
450 /**
451  * struct sof_ipc_pm_ops - IPC-specific PM ops
452  * @ctx_save:           Optional function pointer for context save
453  * @ctx_restore:        Optional function pointer for context restore
454  * @set_core_state:     Optional function pointer for turning on/off a DSP core
455  * @set_pm_gate:        Optional function pointer for pm gate settings
456  */
457 struct sof_ipc_pm_ops {
458         int (*ctx_save)(struct snd_sof_dev *sdev);
459         int (*ctx_restore)(struct snd_sof_dev *sdev);
460         int (*set_core_state)(struct snd_sof_dev *sdev, int core_idx, bool on);
461         int (*set_pm_gate)(struct snd_sof_dev *sdev, u32 flags);
462 };
463
464 /**
465  * struct sof_ipc_fw_loader_ops - IPC/FW-specific loader ops
466  * @validate:           Function pointer for validating the firmware image
467  * @parse_ext_manifest: Function pointer for parsing the manifest of the firmware
468  * @load_fw_to_dsp:     Optional function pointer for loading the firmware to the
469  *                      DSP.
470  *                      The function implements generic, hardware independent way
471  *                      of loading the initial firmware and its modules (if any).
472  */
473 struct sof_ipc_fw_loader_ops {
474         int (*validate)(struct snd_sof_dev *sdev);
475         size_t (*parse_ext_manifest)(struct snd_sof_dev *sdev);
476         int (*load_fw_to_dsp)(struct snd_sof_dev *sdev);
477 };
478
479 struct sof_ipc_tplg_ops;
480 struct sof_ipc_pcm_ops;
481
482 /**
483  * struct sof_ipc_ops - IPC-specific ops
484  * @tplg:       Pointer to IPC-specific topology ops
485  * @pm:         Pointer to PM ops
486  * @pcm:        Pointer to PCM ops
487  * @fw_loader:  Pointer to Firmware Loader ops
488  * @fw_tracing: Optional pointer to Firmware tracing ops
489  *
490  * @init:       Optional pointer for IPC related initialization
491  * @exit:       Optional pointer for IPC related cleanup
492  * @post_fw_boot: Optional pointer to execute IPC related tasks after firmware
493  *              boot.
494  *
495  * @tx_msg:     Function pointer for sending a 'short' IPC message
496  * @set_get_data: Function pointer for set/get data ('large' IPC message). This
497  *              function may split up the 'large' message and use the @tx_msg
498  *              path to transfer individual chunks, or use other means to transfer
499  *              the message.
500  * @get_reply:  Function pointer for fetching the reply to
501  *              sdev->ipc->msg.reply_data
502  * @rx_msg:     Function pointer for handling a received message
503  *
504  * Note: both @tx_msg and @set_get_data considered as TX functions and they are
505  * serialized for the duration of the instructed transfer. A large message sent
506  * via @set_get_data is a single transfer even if at the hardware level it is
507  * handled with multiple chunks.
508  */
509 struct sof_ipc_ops {
510         const struct sof_ipc_tplg_ops *tplg;
511         const struct sof_ipc_pm_ops *pm;
512         const struct sof_ipc_pcm_ops *pcm;
513         const struct sof_ipc_fw_loader_ops *fw_loader;
514         const struct sof_ipc_fw_tracing_ops *fw_tracing;
515
516         int (*init)(struct snd_sof_dev *sdev);
517         void (*exit)(struct snd_sof_dev *sdev);
518         int (*post_fw_boot)(struct snd_sof_dev *sdev);
519
520         int (*tx_msg)(struct snd_sof_dev *sdev, void *msg_data, size_t msg_bytes,
521                       void *reply_data, size_t reply_bytes, bool no_pm);
522         int (*set_get_data)(struct snd_sof_dev *sdev, void *data, size_t data_bytes,
523                             bool set);
524         int (*get_reply)(struct snd_sof_dev *sdev);
525         void (*rx_msg)(struct snd_sof_dev *sdev);
526 };
527
528 /* SOF generic IPC data */
529 struct snd_sof_ipc {
530         struct snd_sof_dev *sdev;
531
532         /* protects messages and the disable flag */
533         struct mutex tx_mutex;
534         /* disables further sending of ipc's */
535         bool disable_ipc_tx;
536
537         /* Maximum allowed size of a single IPC message/reply */
538         size_t max_payload_size;
539
540         struct snd_sof_ipc_msg msg;
541
542         /* IPC ops based on version */
543         const struct sof_ipc_ops *ops;
544 };
545
546 /* Helper to retrieve the IPC ops */
547 #define sof_ipc_get_ops(sdev, ops_name)         \
548                 (((sdev)->ipc && (sdev)->ipc->ops) ? (sdev)->ipc->ops->ops_name : NULL)
549
550 /*
551  * SOF Device Level.
552  */
553 struct snd_sof_dev {
554         struct device *dev;
555         spinlock_t ipc_lock;    /* lock for IPC users */
556         spinlock_t hw_lock;     /* lock for HW IO access */
557
558         /*
559          * When true the DSP is not used.
560          * It is set under the following condition:
561          * User sets the SOF_DBG_DSPLESS_MODE flag in sof_debug module parameter
562          * and
563          * the platform advertises that it can support such mode
564          * pdata->desc->dspless_mode_supported is true.
565          */
566         bool dspless_mode_selected;
567
568         /* Main, Base firmware image */
569         struct sof_firmware basefw;
570
571         /*
572          * ASoC components. plat_drv fields are set dynamically so
573          * can't use const
574          */
575         struct snd_soc_component_driver plat_drv;
576
577         /* current DSP power state */
578         struct sof_dsp_power_state dsp_power_state;
579         /* mutex to protect the dsp_power_state access */
580         struct mutex power_state_access;
581
582         /* Intended power target of system suspend */
583         enum sof_system_suspend_state system_suspend_target;
584
585         /* DSP firmware boot */
586         wait_queue_head_t boot_wait;
587         enum sof_fw_state fw_state;
588         bool first_boot;
589
590         /* work queue in case the probe is implemented in two steps */
591         struct work_struct probe_work;
592         bool probe_completed;
593
594         /* DSP HW differentiation */
595         struct snd_sof_pdata *pdata;
596
597         /* IPC */
598         struct snd_sof_ipc *ipc;
599         struct snd_sof_mailbox fw_info_box;     /* FW shared memory */
600         struct snd_sof_mailbox dsp_box;         /* DSP initiated IPC */
601         struct snd_sof_mailbox host_box;        /* Host initiated IPC */
602         struct snd_sof_mailbox stream_box;      /* Stream position update */
603         struct snd_sof_mailbox debug_box;       /* Debug info updates */
604         struct snd_sof_ipc_msg *msg;
605         int ipc_irq;
606         u32 next_comp_id; /* monotonic - reset during S3 */
607
608         /* memory bases for mmaped DSPs - set by dsp_init() */
609         void __iomem *bar[SND_SOF_BARS];        /* DSP base address */
610         int mmio_bar;
611         int mailbox_bar;
612         size_t dsp_oops_offset;
613
614         /* debug */
615         struct dentry *debugfs_root;
616         struct list_head dfsentry_list;
617         bool dbg_dump_printed;
618         bool ipc_dump_printed;
619         bool d3_prevented; /* runtime pm use count incremented to prevent context lost */
620
621         /* firmware loader */
622         struct sof_ipc_fw_ready fw_ready;
623         struct sof_ipc_fw_version fw_version;
624         struct sof_ipc_cc_version *cc_version;
625
626         /* topology */
627         struct snd_soc_tplg_ops *tplg_ops;
628         struct list_head pcm_list;
629         struct list_head kcontrol_list;
630         struct list_head widget_list;
631         struct list_head pipeline_list;
632         struct list_head dai_list;
633         struct list_head dai_link_list;
634         struct list_head route_list;
635         struct snd_soc_component *component;
636         u32 enabled_cores_mask; /* keep track of enabled cores */
637         bool led_present;
638
639         /* FW configuration */
640         struct sof_ipc_window *info_window;
641
642         /* IPC timeouts in ms */
643         int ipc_timeout;
644         int boot_timeout;
645
646         /* firmwre tracing */
647         bool fw_trace_is_supported; /* set with Kconfig or module parameter */
648         void *fw_trace_data; /* private data used by firmware tracing implementation */
649
650         bool msi_enabled;
651
652         /* DSP core context */
653         u32 num_cores;
654
655         /*
656          * ref count per core that will be modified during system suspend/resume and during pcm
657          * hw_params/hw_free. This doesn't need to be protected with a mutex because pcm
658          * hw_params/hw_free are already protected by the PCM mutex in the ALSA framework in
659          * sound/core/ when streams are active and during system suspend/resume, streams are
660          * already suspended.
661          */
662         int dsp_core_ref_count[SOF_MAX_DSP_NUM_CORES];
663
664         /*
665          * Used to keep track of registered IPC client devices so that they can
666          * be removed when the parent SOF module is removed.
667          */
668         struct list_head ipc_client_list;
669
670         /* mutex to protect client list */
671         struct mutex ipc_client_mutex;
672
673         /*
674          * Used for tracking the IPC client's RX registration for DSP initiated
675          * message handling.
676          */
677         struct list_head ipc_rx_handler_list;
678
679         /*
680          * Used for tracking the IPC client's registration for DSP state change
681          * notification
682          */
683         struct list_head fw_state_handler_list;
684
685         /* to protect the ipc_rx_handler_list  and  dsp_state_handler_list list */
686         struct mutex client_event_handler_mutex;
687
688         /* quirks to override topology values */
689         bool mclk_id_override;
690         u16  mclk_id_quirk; /* same size as in IPC3 definitions */
691
692         void *private;                  /* core does not touch this */
693 };
694
695 /*
696  * Device Level.
697  */
698
699 int snd_sof_device_probe(struct device *dev, struct snd_sof_pdata *plat_data);
700 int snd_sof_device_remove(struct device *dev);
701 int snd_sof_device_shutdown(struct device *dev);
702 bool snd_sof_device_probe_completed(struct device *dev);
703
704 int snd_sof_runtime_suspend(struct device *dev);
705 int snd_sof_runtime_resume(struct device *dev);
706 int snd_sof_runtime_idle(struct device *dev);
707 int snd_sof_resume(struct device *dev);
708 int snd_sof_suspend(struct device *dev);
709 int snd_sof_dsp_power_down_notify(struct snd_sof_dev *sdev);
710 int snd_sof_prepare(struct device *dev);
711 void snd_sof_complete(struct device *dev);
712
713 void snd_sof_new_platform_drv(struct snd_sof_dev *sdev);
714
715 /*
716  * Compress support
717  */
718 extern struct snd_compress_ops sof_compressed_ops;
719
720 /*
721  * Firmware (firmware, libraries, topologies) file location
722  */
723 int sof_create_ipc_file_profile(struct snd_sof_dev *sdev,
724                                 struct sof_loadable_file_profile *base_profile,
725                                 struct sof_loadable_file_profile *out_profile);
726
727 /*
728  * Firmware loading.
729  */
730 int snd_sof_load_firmware_raw(struct snd_sof_dev *sdev);
731 int snd_sof_load_firmware_memcpy(struct snd_sof_dev *sdev);
732 int snd_sof_run_firmware(struct snd_sof_dev *sdev);
733 void snd_sof_fw_unload(struct snd_sof_dev *sdev);
734
735 /*
736  * IPC low level APIs.
737  */
738 struct snd_sof_ipc *snd_sof_ipc_init(struct snd_sof_dev *sdev);
739 void snd_sof_ipc_free(struct snd_sof_dev *sdev);
740 void snd_sof_ipc_get_reply(struct snd_sof_dev *sdev);
741 void snd_sof_ipc_reply(struct snd_sof_dev *sdev, u32 msg_id);
742 static inline void snd_sof_ipc_msgs_rx(struct snd_sof_dev *sdev)
743 {
744         sdev->ipc->ops->rx_msg(sdev);
745 }
746 int sof_ipc_tx_message(struct snd_sof_ipc *ipc, void *msg_data, size_t msg_bytes,
747                        void *reply_data, size_t reply_bytes);
748 static inline int sof_ipc_tx_message_no_reply(struct snd_sof_ipc *ipc, void *msg_data,
749                                               size_t msg_bytes)
750 {
751         return sof_ipc_tx_message(ipc, msg_data, msg_bytes, NULL, 0);
752 }
753 int sof_ipc_set_get_data(struct snd_sof_ipc *ipc, void *msg_data,
754                          size_t msg_bytes, bool set);
755 int sof_ipc_tx_message_no_pm(struct snd_sof_ipc *ipc, void *msg_data, size_t msg_bytes,
756                              void *reply_data, size_t reply_bytes);
757 static inline int sof_ipc_tx_message_no_pm_no_reply(struct snd_sof_ipc *ipc, void *msg_data,
758                                                     size_t msg_bytes)
759 {
760         return sof_ipc_tx_message_no_pm(ipc, msg_data, msg_bytes, NULL, 0);
761 }
762 int sof_ipc_send_msg(struct snd_sof_dev *sdev, void *msg_data, size_t msg_bytes,
763                      size_t reply_bytes);
764
765 static inline void snd_sof_ipc_process_reply(struct snd_sof_dev *sdev, u32 msg_id)
766 {
767         snd_sof_ipc_get_reply(sdev);
768         snd_sof_ipc_reply(sdev, msg_id);
769 }
770
771 /*
772  * Trace/debug
773  */
774 int snd_sof_dbg_init(struct snd_sof_dev *sdev);
775 void snd_sof_free_debug(struct snd_sof_dev *sdev);
776 int snd_sof_debugfs_buf_item(struct snd_sof_dev *sdev,
777                              void *base, size_t size,
778                              const char *name, mode_t mode);
779 void sof_print_oops_and_stack(struct snd_sof_dev *sdev, const char *level,
780                               u32 panic_code, u32 tracep_code, void *oops,
781                               struct sof_ipc_panic_info *panic_info,
782                               void *stack, size_t stack_words);
783 void snd_sof_handle_fw_exception(struct snd_sof_dev *sdev, const char *msg);
784 int snd_sof_dbg_memory_info_init(struct snd_sof_dev *sdev);
785 int snd_sof_debugfs_add_region_item_iomem(struct snd_sof_dev *sdev,
786                 enum snd_sof_fw_blk_type blk_type, u32 offset, size_t size,
787                 const char *name, enum sof_debugfs_access_type access_type);
788 /* Firmware tracing */
789 int sof_fw_trace_init(struct snd_sof_dev *sdev);
790 void sof_fw_trace_free(struct snd_sof_dev *sdev);
791 void sof_fw_trace_fw_crashed(struct snd_sof_dev *sdev);
792 void sof_fw_trace_suspend(struct snd_sof_dev *sdev, pm_message_t pm_state);
793 int sof_fw_trace_resume(struct snd_sof_dev *sdev);
794
795 /*
796  * DSP Architectures.
797  */
798 static inline void sof_stack(struct snd_sof_dev *sdev, const char *level,
799                              void *oops, u32 *stack, u32 stack_words)
800 {
801                 sof_dsp_arch_ops(sdev)->dsp_stack(sdev, level,  oops, stack,
802                                                   stack_words);
803 }
804
805 static inline void sof_oops(struct snd_sof_dev *sdev, const char *level, void *oops)
806 {
807         if (sof_dsp_arch_ops(sdev)->dsp_oops)
808                 sof_dsp_arch_ops(sdev)->dsp_oops(sdev, level, oops);
809 }
810
811 extern const struct dsp_arch_ops sof_xtensa_arch_ops;
812
813 /*
814  * Firmware state tracking
815  */
816 void sof_set_fw_state(struct snd_sof_dev *sdev, enum sof_fw_state new_state);
817
818 /*
819  * Utilities
820  */
821 void sof_io_write(struct snd_sof_dev *sdev, void __iomem *addr, u32 value);
822 void sof_io_write64(struct snd_sof_dev *sdev, void __iomem *addr, u64 value);
823 u32 sof_io_read(struct snd_sof_dev *sdev, void __iomem *addr);
824 u64 sof_io_read64(struct snd_sof_dev *sdev, void __iomem *addr);
825 void sof_mailbox_write(struct snd_sof_dev *sdev, u32 offset,
826                        void *message, size_t bytes);
827 void sof_mailbox_read(struct snd_sof_dev *sdev, u32 offset,
828                       void *message, size_t bytes);
829 int sof_block_write(struct snd_sof_dev *sdev, enum snd_sof_fw_blk_type blk_type,
830                     u32 offset, void *src, size_t size);
831 int sof_block_read(struct snd_sof_dev *sdev, enum snd_sof_fw_blk_type blk_type,
832                    u32 offset, void *dest, size_t size);
833
834 int sof_ipc_msg_data(struct snd_sof_dev *sdev,
835                      struct snd_sof_pcm_stream *sps,
836                      void *p, size_t sz);
837 int sof_set_stream_data_offset(struct snd_sof_dev *sdev,
838                                struct snd_sof_pcm_stream *sps,
839                                size_t posn_offset);
840
841 int sof_stream_pcm_open(struct snd_sof_dev *sdev,
842                         struct snd_pcm_substream *substream);
843 int sof_stream_pcm_close(struct snd_sof_dev *sdev,
844                          struct snd_pcm_substream *substream);
845
846 /* SOF client support */
847 #if IS_ENABLED(CONFIG_SND_SOC_SOF_CLIENT)
848 int sof_client_dev_register(struct snd_sof_dev *sdev, const char *name, u32 id,
849                             const void *data, size_t size);
850 void sof_client_dev_unregister(struct snd_sof_dev *sdev, const char *name, u32 id);
851 int sof_register_clients(struct snd_sof_dev *sdev);
852 void sof_unregister_clients(struct snd_sof_dev *sdev);
853 void sof_client_ipc_rx_dispatcher(struct snd_sof_dev *sdev, void *msg_buf);
854 void sof_client_fw_state_dispatcher(struct snd_sof_dev *sdev);
855 int sof_suspend_clients(struct snd_sof_dev *sdev, pm_message_t state);
856 int sof_resume_clients(struct snd_sof_dev *sdev);
857 #else /* CONFIG_SND_SOC_SOF_CLIENT */
858 static inline int sof_client_dev_register(struct snd_sof_dev *sdev, const char *name,
859                                           u32 id, const void *data, size_t size)
860 {
861         return 0;
862 }
863
864 static inline void sof_client_dev_unregister(struct snd_sof_dev *sdev,
865                                              const char *name, u32 id)
866 {
867 }
868
869 static inline int sof_register_clients(struct snd_sof_dev *sdev)
870 {
871         return 0;
872 }
873
874 static inline  void sof_unregister_clients(struct snd_sof_dev *sdev)
875 {
876 }
877
878 static inline void sof_client_ipc_rx_dispatcher(struct snd_sof_dev *sdev, void *msg_buf)
879 {
880 }
881
882 static inline void sof_client_fw_state_dispatcher(struct snd_sof_dev *sdev)
883 {
884 }
885
886 static inline int sof_suspend_clients(struct snd_sof_dev *sdev, pm_message_t state)
887 {
888         return 0;
889 }
890
891 static inline int sof_resume_clients(struct snd_sof_dev *sdev)
892 {
893         return 0;
894 }
895 #endif /* CONFIG_SND_SOC_SOF_CLIENT */
896
897 /* Main ops for IPC implementations */
898 extern const struct sof_ipc_ops ipc3_ops;
899 extern const struct sof_ipc_ops ipc4_ops;
900
901 #endif