1 /* SPDX-License-Identifier: (GPL-2.0-only 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_IO_H
12 #define __SOUND_SOC_SOF_IO_H
14 #include <linux/device.h>
15 #include <linux/interrupt.h>
16 #include <linux/kernel.h>
17 #include <linux/types.h>
18 #include <sound/pcm.h>
21 #define sof_ops(sdev) \
22 ((sdev)->pdata->desc->ops)
24 /* Mandatory operations are verified during probing */
27 static inline int snd_sof_probe(struct snd_sof_dev *sdev)
29 return sof_ops(sdev)->probe(sdev);
32 static inline int snd_sof_remove(struct snd_sof_dev *sdev)
34 if (sof_ops(sdev)->remove)
35 return sof_ops(sdev)->remove(sdev);
40 static inline int snd_sof_shutdown(struct snd_sof_dev *sdev)
42 if (sof_ops(sdev)->shutdown)
43 return sof_ops(sdev)->shutdown(sdev);
51 * snd_sof_dsp_run returns the core mask of the cores that are available
52 * after successful fw boot
54 static inline int snd_sof_dsp_run(struct snd_sof_dev *sdev)
56 return sof_ops(sdev)->run(sdev);
59 static inline int snd_sof_dsp_stall(struct snd_sof_dev *sdev, unsigned int core_mask)
61 if (sof_ops(sdev)->stall)
62 return sof_ops(sdev)->stall(sdev, core_mask);
67 static inline int snd_sof_dsp_reset(struct snd_sof_dev *sdev)
69 if (sof_ops(sdev)->reset)
70 return sof_ops(sdev)->reset(sdev);
75 /* dsp core power up/power down */
76 static inline int snd_sof_dsp_core_power_up(struct snd_sof_dev *sdev,
77 unsigned int core_mask)
81 core_mask &= ~sdev->enabled_cores_mask;
82 if (sof_ops(sdev)->core_power_up && core_mask) {
83 ret = sof_ops(sdev)->core_power_up(sdev, core_mask);
85 sdev->enabled_cores_mask |= core_mask;
91 static inline int snd_sof_dsp_core_power_down(struct snd_sof_dev *sdev,
92 unsigned int core_mask)
96 core_mask &= sdev->enabled_cores_mask;
97 if (sof_ops(sdev)->core_power_down && core_mask) {
98 ret = sof_ops(sdev)->core_power_down(sdev, core_mask);
100 sdev->enabled_cores_mask &= ~core_mask;
106 /* pre/post fw load */
107 static inline int snd_sof_dsp_pre_fw_run(struct snd_sof_dev *sdev)
109 if (sof_ops(sdev)->pre_fw_run)
110 return sof_ops(sdev)->pre_fw_run(sdev);
115 static inline int snd_sof_dsp_post_fw_run(struct snd_sof_dev *sdev)
117 if (sof_ops(sdev)->post_fw_run)
118 return sof_ops(sdev)->post_fw_run(sdev);
123 /* parse platform specific extended manifest */
124 static inline int snd_sof_dsp_parse_platform_ext_manifest(struct snd_sof_dev *sdev,
125 const struct sof_ext_man_elem_header *hdr)
127 if (sof_ops(sdev)->parse_platform_ext_manifest)
128 return sof_ops(sdev)->parse_platform_ext_manifest(sdev, hdr);
136 * snd_sof_dsp_get_bar_index - Maps a section type with a BAR index
139 * @type: section type as described by snd_sof_fw_blk_type
141 * Returns the corresponding BAR index (a positive integer) or -EINVAL
142 * in case there is no mapping
144 static inline int snd_sof_dsp_get_bar_index(struct snd_sof_dev *sdev, u32 type)
146 if (sof_ops(sdev)->get_bar_index)
147 return sof_ops(sdev)->get_bar_index(sdev, type);
149 return sdev->mmio_bar;
152 static inline int snd_sof_dsp_get_mailbox_offset(struct snd_sof_dev *sdev)
154 if (sof_ops(sdev)->get_mailbox_offset)
155 return sof_ops(sdev)->get_mailbox_offset(sdev);
157 dev_err(sdev->dev, "error: %s not defined\n", __func__);
161 static inline int snd_sof_dsp_get_window_offset(struct snd_sof_dev *sdev,
164 if (sof_ops(sdev)->get_window_offset)
165 return sof_ops(sdev)->get_window_offset(sdev, id);
167 dev_err(sdev->dev, "error: %s not defined\n", __func__);
170 /* power management */
171 static inline int snd_sof_dsp_resume(struct snd_sof_dev *sdev)
173 if (sof_ops(sdev)->resume)
174 return sof_ops(sdev)->resume(sdev);
179 static inline int snd_sof_dsp_suspend(struct snd_sof_dev *sdev,
182 if (sof_ops(sdev)->suspend)
183 return sof_ops(sdev)->suspend(sdev, target_state);
188 static inline int snd_sof_dsp_runtime_resume(struct snd_sof_dev *sdev)
190 if (sof_ops(sdev)->runtime_resume)
191 return sof_ops(sdev)->runtime_resume(sdev);
196 static inline int snd_sof_dsp_runtime_suspend(struct snd_sof_dev *sdev)
198 if (sof_ops(sdev)->runtime_suspend)
199 return sof_ops(sdev)->runtime_suspend(sdev);
204 static inline int snd_sof_dsp_runtime_idle(struct snd_sof_dev *sdev)
206 if (sof_ops(sdev)->runtime_idle)
207 return sof_ops(sdev)->runtime_idle(sdev);
212 static inline int snd_sof_dsp_hw_params_upon_resume(struct snd_sof_dev *sdev)
214 if (sof_ops(sdev)->set_hw_params_upon_resume)
215 return sof_ops(sdev)->set_hw_params_upon_resume(sdev);
219 static inline int snd_sof_dsp_set_clk(struct snd_sof_dev *sdev, u32 freq)
221 if (sof_ops(sdev)->set_clk)
222 return sof_ops(sdev)->set_clk(sdev, freq);
228 snd_sof_dsp_set_power_state(struct snd_sof_dev *sdev,
229 const struct sof_dsp_power_state *target_state)
233 mutex_lock(&sdev->power_state_access);
235 if (sof_ops(sdev)->set_power_state)
236 ret = sof_ops(sdev)->set_power_state(sdev, target_state);
238 mutex_unlock(&sdev->power_state_access);
244 static inline void snd_sof_dsp_dbg_dump(struct snd_sof_dev *sdev, u32 flags)
246 if (sof_ops(sdev)->dbg_dump)
247 sof_ops(sdev)->dbg_dump(sdev, flags);
250 static inline void snd_sof_ipc_dump(struct snd_sof_dev *sdev)
252 if (sof_ops(sdev)->ipc_dump)
253 sof_ops(sdev)->ipc_dump(sdev);
257 static inline void snd_sof_dsp_write(struct snd_sof_dev *sdev, u32 bar,
258 u32 offset, u32 value)
260 if (sof_ops(sdev)->write) {
261 sof_ops(sdev)->write(sdev, sdev->bar[bar] + offset, value);
265 dev_err_ratelimited(sdev->dev, "error: %s not defined\n", __func__);
268 static inline void snd_sof_dsp_write64(struct snd_sof_dev *sdev, u32 bar,
269 u32 offset, u64 value)
271 if (sof_ops(sdev)->write64) {
272 sof_ops(sdev)->write64(sdev, sdev->bar[bar] + offset, value);
276 dev_err_ratelimited(sdev->dev, "error: %s not defined\n", __func__);
279 static inline u32 snd_sof_dsp_read(struct snd_sof_dev *sdev, u32 bar,
282 if (sof_ops(sdev)->read)
283 return sof_ops(sdev)->read(sdev, sdev->bar[bar] + offset);
285 dev_err(sdev->dev, "error: %s not defined\n", __func__);
289 static inline u64 snd_sof_dsp_read64(struct snd_sof_dev *sdev, u32 bar,
292 if (sof_ops(sdev)->read64)
293 return sof_ops(sdev)->read64(sdev, sdev->bar[bar] + offset);
295 dev_err(sdev->dev, "error: %s not defined\n", __func__);
300 static inline void snd_sof_dsp_block_read(struct snd_sof_dev *sdev, u32 bar,
301 u32 offset, void *dest, size_t bytes)
303 sof_ops(sdev)->block_read(sdev, bar, offset, dest, bytes);
306 static inline void snd_sof_dsp_block_write(struct snd_sof_dev *sdev, u32 bar,
307 u32 offset, void *src, size_t bytes)
309 sof_ops(sdev)->block_write(sdev, bar, offset, src, bytes);
313 static inline int snd_sof_dsp_send_msg(struct snd_sof_dev *sdev,
314 struct snd_sof_ipc_msg *msg)
316 return sof_ops(sdev)->send_msg(sdev, msg);
320 static inline int snd_sof_dma_trace_init(struct snd_sof_dev *sdev,
323 if (sof_ops(sdev)->trace_init)
324 return sof_ops(sdev)->trace_init(sdev, stream_tag);
329 static inline int snd_sof_dma_trace_release(struct snd_sof_dev *sdev)
331 if (sof_ops(sdev)->trace_release)
332 return sof_ops(sdev)->trace_release(sdev);
337 static inline int snd_sof_dma_trace_trigger(struct snd_sof_dev *sdev, int cmd)
339 if (sof_ops(sdev)->trace_trigger)
340 return sof_ops(sdev)->trace_trigger(sdev, cmd);
347 snd_sof_pcm_platform_open(struct snd_sof_dev *sdev,
348 struct snd_pcm_substream *substream)
350 if (sof_ops(sdev) && sof_ops(sdev)->pcm_open)
351 return sof_ops(sdev)->pcm_open(sdev, substream);
356 /* disconnect pcm substream to a host stream */
358 snd_sof_pcm_platform_close(struct snd_sof_dev *sdev,
359 struct snd_pcm_substream *substream)
361 if (sof_ops(sdev) && sof_ops(sdev)->pcm_close)
362 return sof_ops(sdev)->pcm_close(sdev, substream);
367 /* host stream hw params */
369 snd_sof_pcm_platform_hw_params(struct snd_sof_dev *sdev,
370 struct snd_pcm_substream *substream,
371 struct snd_pcm_hw_params *params,
372 struct sof_ipc_stream_params *ipc_params)
374 if (sof_ops(sdev) && sof_ops(sdev)->pcm_hw_params)
375 return sof_ops(sdev)->pcm_hw_params(sdev, substream,
381 /* host stream hw free */
383 snd_sof_pcm_platform_hw_free(struct snd_sof_dev *sdev,
384 struct snd_pcm_substream *substream)
386 if (sof_ops(sdev) && sof_ops(sdev)->pcm_hw_free)
387 return sof_ops(sdev)->pcm_hw_free(sdev, substream);
392 /* host stream trigger */
394 snd_sof_pcm_platform_trigger(struct snd_sof_dev *sdev,
395 struct snd_pcm_substream *substream, int cmd)
397 if (sof_ops(sdev) && sof_ops(sdev)->pcm_trigger)
398 return sof_ops(sdev)->pcm_trigger(sdev, substream, cmd);
403 /* host DSP message data */
404 static inline void snd_sof_ipc_msg_data(struct snd_sof_dev *sdev,
405 struct snd_pcm_substream *substream,
408 sof_ops(sdev)->ipc_msg_data(sdev, substream, p, sz);
411 /* host configure DSP HW parameters */
413 snd_sof_ipc_pcm_params(struct snd_sof_dev *sdev,
414 struct snd_pcm_substream *substream,
415 const struct sof_ipc_pcm_params_reply *reply)
417 return sof_ops(sdev)->ipc_pcm_params(sdev, substream, reply);
420 /* host stream pointer */
421 static inline snd_pcm_uframes_t
422 snd_sof_pcm_platform_pointer(struct snd_sof_dev *sdev,
423 struct snd_pcm_substream *substream)
425 if (sof_ops(sdev) && sof_ops(sdev)->pcm_pointer)
426 return sof_ops(sdev)->pcm_pointer(sdev, substream);
431 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_PROBES)
433 snd_sof_probe_compr_assign(struct snd_sof_dev *sdev,
434 struct snd_compr_stream *cstream, struct snd_soc_dai *dai)
436 return sof_ops(sdev)->probe_assign(sdev, cstream, dai);
440 snd_sof_probe_compr_free(struct snd_sof_dev *sdev,
441 struct snd_compr_stream *cstream, struct snd_soc_dai *dai)
443 return sof_ops(sdev)->probe_free(sdev, cstream, dai);
447 snd_sof_probe_compr_set_params(struct snd_sof_dev *sdev,
448 struct snd_compr_stream *cstream,
449 struct snd_compr_params *params, struct snd_soc_dai *dai)
451 return sof_ops(sdev)->probe_set_params(sdev, cstream, params, dai);
455 snd_sof_probe_compr_trigger(struct snd_sof_dev *sdev,
456 struct snd_compr_stream *cstream, int cmd,
457 struct snd_soc_dai *dai)
459 return sof_ops(sdev)->probe_trigger(sdev, cstream, cmd, dai);
463 snd_sof_probe_compr_pointer(struct snd_sof_dev *sdev,
464 struct snd_compr_stream *cstream,
465 struct snd_compr_tstamp *tstamp, struct snd_soc_dai *dai)
467 if (sof_ops(sdev) && sof_ops(sdev)->probe_pointer)
468 return sof_ops(sdev)->probe_pointer(sdev, cstream, tstamp, dai);
476 snd_sof_machine_register(struct snd_sof_dev *sdev, void *pdata)
478 if (sof_ops(sdev) && sof_ops(sdev)->machine_register)
479 return sof_ops(sdev)->machine_register(sdev, pdata);
485 snd_sof_machine_unregister(struct snd_sof_dev *sdev, void *pdata)
487 if (sof_ops(sdev) && sof_ops(sdev)->machine_unregister)
488 sof_ops(sdev)->machine_unregister(sdev, pdata);
492 snd_sof_machine_select(struct snd_sof_dev *sdev)
494 if (sof_ops(sdev) && sof_ops(sdev)->machine_select)
495 sof_ops(sdev)->machine_select(sdev);
499 snd_sof_set_mach_params(const struct snd_soc_acpi_mach *mach,
500 struct snd_sof_dev *sdev)
502 if (sof_ops(sdev) && sof_ops(sdev)->set_mach_params)
503 sof_ops(sdev)->set_mach_params(mach, sdev);
506 static inline const struct snd_sof_dsp_ops
507 *sof_get_ops(const struct sof_dev_desc *d,
508 const struct sof_ops_table mach_ops[], int asize)
512 for (i = 0; i < asize; i++) {
513 if (d == mach_ops[i].desc)
514 return mach_ops[i].ops;
522 * snd_sof_dsp_register_poll_timeout - Periodically poll an address
523 * until a condition is met or a timeout occurs
524 * @op: accessor function (takes @addr as its only argument)
525 * @addr: Address to poll
526 * @val: Variable to read the value into
527 * @cond: Break condition (usually involving @val)
528 * @sleep_us: Maximum time to sleep between reads in us (0
529 * tight-loops). Should be less than ~20ms since usleep_range
530 * is used (see Documentation/timers/timers-howto.rst).
531 * @timeout_us: Timeout in us, 0 means never timeout
533 * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
534 * case, the last read value at @addr is stored in @val. Must not
535 * be called from atomic context if sleep_us or timeout_us are used.
537 * This is modelled after the readx_poll_timeout macros in linux/iopoll.h.
539 #define snd_sof_dsp_read_poll_timeout(sdev, bar, offset, val, cond, sleep_us, timeout_us) \
541 u64 __timeout_us = (timeout_us); \
542 unsigned long __sleep_us = (sleep_us); \
543 ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
544 might_sleep_if((__sleep_us) != 0); \
546 (val) = snd_sof_dsp_read(sdev, bar, offset); \
549 "FW Poll Status: reg[%#x]=%#x successful\n", \
553 if (__timeout_us && \
554 ktime_compare(ktime_get(), __timeout) > 0) { \
555 (val) = snd_sof_dsp_read(sdev, bar, offset); \
557 "FW Poll Status: reg[%#x]=%#x timedout\n", \
562 usleep_range((__sleep_us >> 2) + 1, __sleep_us); \
564 (cond) ? 0 : -ETIMEDOUT; \
567 /* This is for registers bits with attribute RWC */
568 bool snd_sof_pci_update_bits(struct snd_sof_dev *sdev, u32 offset,
569 u32 mask, u32 value);
571 bool snd_sof_dsp_update_bits_unlocked(struct snd_sof_dev *sdev, u32 bar,
572 u32 offset, u32 mask, u32 value);
574 bool snd_sof_dsp_update_bits64_unlocked(struct snd_sof_dev *sdev, u32 bar,
575 u32 offset, u64 mask, u64 value);
577 bool snd_sof_dsp_update_bits(struct snd_sof_dev *sdev, u32 bar, u32 offset,
578 u32 mask, u32 value);
580 bool snd_sof_dsp_update_bits64(struct snd_sof_dev *sdev, u32 bar,
581 u32 offset, u64 mask, u64 value);
583 void snd_sof_dsp_update_bits_forced(struct snd_sof_dev *sdev, u32 bar,
584 u32 offset, u32 mask, u32 value);
586 int snd_sof_dsp_register_poll(struct snd_sof_dev *sdev, u32 bar, u32 offset,
587 u32 mask, u32 target, u32 timeout_ms,
590 void snd_sof_dsp_panic(struct snd_sof_dev *sdev, u32 offset);