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