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 static inline int sof_ops_init(struct snd_sof_dev *sdev)
26 if (sdev->pdata->desc->ops_init)
27 return sdev->pdata->desc->ops_init(sdev);
32 static inline void sof_ops_free(struct snd_sof_dev *sdev)
34 if (sdev->pdata->desc->ops_free)
35 sdev->pdata->desc->ops_free(sdev);
38 /* Mandatory operations are verified during probing */
41 static inline int snd_sof_probe(struct snd_sof_dev *sdev)
43 return sof_ops(sdev)->probe(sdev);
46 static inline int snd_sof_remove(struct snd_sof_dev *sdev)
48 if (sof_ops(sdev)->remove)
49 return sof_ops(sdev)->remove(sdev);
54 static inline int snd_sof_shutdown(struct snd_sof_dev *sdev)
56 if (sof_ops(sdev)->shutdown)
57 return sof_ops(sdev)->shutdown(sdev);
65 * snd_sof_dsp_run returns the core mask of the cores that are available
66 * after successful fw boot
68 static inline int snd_sof_dsp_run(struct snd_sof_dev *sdev)
70 return sof_ops(sdev)->run(sdev);
73 static inline int snd_sof_dsp_stall(struct snd_sof_dev *sdev, unsigned int core_mask)
75 if (sof_ops(sdev)->stall)
76 return sof_ops(sdev)->stall(sdev, core_mask);
81 static inline int snd_sof_dsp_reset(struct snd_sof_dev *sdev)
83 if (sof_ops(sdev)->reset)
84 return sof_ops(sdev)->reset(sdev);
89 /* dsp core get/put */
90 static inline int snd_sof_dsp_core_get(struct snd_sof_dev *sdev, int core)
92 if (core > sdev->num_cores - 1) {
93 dev_err(sdev->dev, "invalid core id: %d for num_cores: %d\n", core,
98 if (sof_ops(sdev)->core_get) {
101 /* if current ref_count is > 0, increment it and return */
102 if (sdev->dsp_core_ref_count[core] > 0) {
103 sdev->dsp_core_ref_count[core]++;
107 /* power up the core */
108 ret = sof_ops(sdev)->core_get(sdev, core);
112 /* increment ref_count */
113 sdev->dsp_core_ref_count[core]++;
115 /* and update enabled_cores_mask */
116 sdev->enabled_cores_mask |= BIT(core);
118 dev_dbg(sdev->dev, "Core %d powered up\n", core);
124 static inline int snd_sof_dsp_core_put(struct snd_sof_dev *sdev, int core)
126 if (core > sdev->num_cores - 1) {
127 dev_err(sdev->dev, "invalid core id: %d for num_cores: %d\n", core,
132 if (sof_ops(sdev)->core_put) {
135 /* decrement ref_count and return if it is > 0 */
136 if (--(sdev->dsp_core_ref_count[core]) > 0)
139 /* power down the core */
140 ret = sof_ops(sdev)->core_put(sdev, core);
144 /* and update enabled_cores_mask */
145 sdev->enabled_cores_mask &= ~BIT(core);
147 dev_dbg(sdev->dev, "Core %d powered down\n", core);
153 /* pre/post fw load */
154 static inline int snd_sof_dsp_pre_fw_run(struct snd_sof_dev *sdev)
156 if (sof_ops(sdev)->pre_fw_run)
157 return sof_ops(sdev)->pre_fw_run(sdev);
162 static inline int snd_sof_dsp_post_fw_run(struct snd_sof_dev *sdev)
164 if (sof_ops(sdev)->post_fw_run)
165 return sof_ops(sdev)->post_fw_run(sdev);
170 /* parse platform specific extended manifest */
171 static inline int snd_sof_dsp_parse_platform_ext_manifest(struct snd_sof_dev *sdev,
172 const struct sof_ext_man_elem_header *hdr)
174 if (sof_ops(sdev)->parse_platform_ext_manifest)
175 return sof_ops(sdev)->parse_platform_ext_manifest(sdev, hdr);
183 * snd_sof_dsp_get_bar_index - Maps a section type with a BAR index
186 * @type: section type as described by snd_sof_fw_blk_type
188 * Returns the corresponding BAR index (a positive integer) or -EINVAL
189 * in case there is no mapping
191 static inline int snd_sof_dsp_get_bar_index(struct snd_sof_dev *sdev, u32 type)
193 if (sof_ops(sdev)->get_bar_index)
194 return sof_ops(sdev)->get_bar_index(sdev, type);
196 return sdev->mmio_bar;
199 static inline int snd_sof_dsp_get_mailbox_offset(struct snd_sof_dev *sdev)
201 if (sof_ops(sdev)->get_mailbox_offset)
202 return sof_ops(sdev)->get_mailbox_offset(sdev);
204 dev_err(sdev->dev, "error: %s not defined\n", __func__);
208 static inline int snd_sof_dsp_get_window_offset(struct snd_sof_dev *sdev,
211 if (sof_ops(sdev)->get_window_offset)
212 return sof_ops(sdev)->get_window_offset(sdev, id);
214 dev_err(sdev->dev, "error: %s not defined\n", __func__);
217 /* power management */
218 static inline int snd_sof_dsp_resume(struct snd_sof_dev *sdev)
220 if (sof_ops(sdev)->resume)
221 return sof_ops(sdev)->resume(sdev);
226 static inline int snd_sof_dsp_suspend(struct snd_sof_dev *sdev,
229 if (sof_ops(sdev)->suspend)
230 return sof_ops(sdev)->suspend(sdev, target_state);
235 static inline int snd_sof_dsp_runtime_resume(struct snd_sof_dev *sdev)
237 if (sof_ops(sdev)->runtime_resume)
238 return sof_ops(sdev)->runtime_resume(sdev);
243 static inline int snd_sof_dsp_runtime_suspend(struct snd_sof_dev *sdev)
245 if (sof_ops(sdev)->runtime_suspend)
246 return sof_ops(sdev)->runtime_suspend(sdev);
251 static inline int snd_sof_dsp_runtime_idle(struct snd_sof_dev *sdev)
253 if (sof_ops(sdev)->runtime_idle)
254 return sof_ops(sdev)->runtime_idle(sdev);
259 static inline int snd_sof_dsp_hw_params_upon_resume(struct snd_sof_dev *sdev)
261 if (sof_ops(sdev)->set_hw_params_upon_resume)
262 return sof_ops(sdev)->set_hw_params_upon_resume(sdev);
266 static inline int snd_sof_dsp_set_clk(struct snd_sof_dev *sdev, u32 freq)
268 if (sof_ops(sdev)->set_clk)
269 return sof_ops(sdev)->set_clk(sdev, freq);
275 snd_sof_dsp_set_power_state(struct snd_sof_dev *sdev,
276 const struct sof_dsp_power_state *target_state)
280 mutex_lock(&sdev->power_state_access);
282 if (sof_ops(sdev)->set_power_state)
283 ret = sof_ops(sdev)->set_power_state(sdev, target_state);
285 mutex_unlock(&sdev->power_state_access);
291 void snd_sof_dsp_dbg_dump(struct snd_sof_dev *sdev, const char *msg, u32 flags);
293 static inline int snd_sof_debugfs_add_region_item(struct snd_sof_dev *sdev,
294 enum snd_sof_fw_blk_type blk_type, u32 offset, size_t size,
295 const char *name, enum sof_debugfs_access_type access_type)
297 if (sof_ops(sdev) && sof_ops(sdev)->debugfs_add_region_item)
298 return sof_ops(sdev)->debugfs_add_region_item(sdev, blk_type, offset,
299 size, name, access_type);
305 static inline void snd_sof_dsp_write(struct snd_sof_dev *sdev, u32 bar,
306 u32 offset, u32 value)
308 if (sof_ops(sdev)->write) {
309 sof_ops(sdev)->write(sdev, sdev->bar[bar] + offset, value);
313 dev_err_ratelimited(sdev->dev, "error: %s not defined\n", __func__);
316 static inline void snd_sof_dsp_write64(struct snd_sof_dev *sdev, u32 bar,
317 u32 offset, u64 value)
319 if (sof_ops(sdev)->write64) {
320 sof_ops(sdev)->write64(sdev, sdev->bar[bar] + offset, value);
324 dev_err_ratelimited(sdev->dev, "error: %s not defined\n", __func__);
327 static inline u32 snd_sof_dsp_read(struct snd_sof_dev *sdev, u32 bar,
330 if (sof_ops(sdev)->read)
331 return sof_ops(sdev)->read(sdev, sdev->bar[bar] + offset);
333 dev_err(sdev->dev, "error: %s not defined\n", __func__);
337 static inline u64 snd_sof_dsp_read64(struct snd_sof_dev *sdev, u32 bar,
340 if (sof_ops(sdev)->read64)
341 return sof_ops(sdev)->read64(sdev, sdev->bar[bar] + offset);
343 dev_err(sdev->dev, "error: %s not defined\n", __func__);
348 static inline int snd_sof_dsp_block_read(struct snd_sof_dev *sdev,
349 enum snd_sof_fw_blk_type blk_type,
350 u32 offset, void *dest, size_t bytes)
352 return sof_ops(sdev)->block_read(sdev, blk_type, offset, dest, bytes);
355 static inline int snd_sof_dsp_block_write(struct snd_sof_dev *sdev,
356 enum snd_sof_fw_blk_type blk_type,
357 u32 offset, void *src, size_t bytes)
359 return sof_ops(sdev)->block_write(sdev, blk_type, offset, src, bytes);
363 static inline void snd_sof_dsp_mailbox_read(struct snd_sof_dev *sdev,
364 u32 offset, void *dest, size_t bytes)
366 if (sof_ops(sdev)->mailbox_read)
367 sof_ops(sdev)->mailbox_read(sdev, offset, dest, bytes);
370 static inline void snd_sof_dsp_mailbox_write(struct snd_sof_dev *sdev,
371 u32 offset, void *src, size_t bytes)
373 if (sof_ops(sdev)->mailbox_write)
374 sof_ops(sdev)->mailbox_write(sdev, offset, src, bytes);
378 static inline int snd_sof_dsp_send_msg(struct snd_sof_dev *sdev,
379 struct snd_sof_ipc_msg *msg)
381 return sof_ops(sdev)->send_msg(sdev, msg);
386 snd_sof_pcm_platform_open(struct snd_sof_dev *sdev,
387 struct snd_pcm_substream *substream)
389 if (sof_ops(sdev) && sof_ops(sdev)->pcm_open)
390 return sof_ops(sdev)->pcm_open(sdev, substream);
395 /* disconnect pcm substream to a host stream */
397 snd_sof_pcm_platform_close(struct snd_sof_dev *sdev,
398 struct snd_pcm_substream *substream)
400 if (sof_ops(sdev) && sof_ops(sdev)->pcm_close)
401 return sof_ops(sdev)->pcm_close(sdev, substream);
406 /* host stream hw params */
408 snd_sof_pcm_platform_hw_params(struct snd_sof_dev *sdev,
409 struct snd_pcm_substream *substream,
410 struct snd_pcm_hw_params *params,
411 struct snd_sof_platform_stream_params *platform_params)
413 if (sof_ops(sdev) && sof_ops(sdev)->pcm_hw_params)
414 return sof_ops(sdev)->pcm_hw_params(sdev, substream, params,
420 /* host stream hw free */
422 snd_sof_pcm_platform_hw_free(struct snd_sof_dev *sdev,
423 struct snd_pcm_substream *substream)
425 if (sof_ops(sdev) && sof_ops(sdev)->pcm_hw_free)
426 return sof_ops(sdev)->pcm_hw_free(sdev, substream);
431 /* host stream trigger */
433 snd_sof_pcm_platform_trigger(struct snd_sof_dev *sdev,
434 struct snd_pcm_substream *substream, int cmd)
436 if (sof_ops(sdev) && sof_ops(sdev)->pcm_trigger)
437 return sof_ops(sdev)->pcm_trigger(sdev, substream, cmd);
442 /* Firmware loading */
443 static inline int snd_sof_load_firmware(struct snd_sof_dev *sdev)
445 dev_dbg(sdev->dev, "loading firmware\n");
447 return sof_ops(sdev)->load_firmware(sdev);
450 /* host DSP message data */
451 static inline int snd_sof_ipc_msg_data(struct snd_sof_dev *sdev,
452 struct snd_pcm_substream *substream,
455 return sof_ops(sdev)->ipc_msg_data(sdev, substream, p, sz);
457 /* host side configuration of the stream's data offset in stream mailbox area */
459 snd_sof_set_stream_data_offset(struct snd_sof_dev *sdev,
460 struct snd_pcm_substream *substream,
463 if (sof_ops(sdev) && sof_ops(sdev)->set_stream_data_offset)
464 return sof_ops(sdev)->set_stream_data_offset(sdev, substream,
470 /* host stream pointer */
471 static inline snd_pcm_uframes_t
472 snd_sof_pcm_platform_pointer(struct snd_sof_dev *sdev,
473 struct snd_pcm_substream *substream)
475 if (sof_ops(sdev) && sof_ops(sdev)->pcm_pointer)
476 return sof_ops(sdev)->pcm_pointer(sdev, substream);
482 static inline int snd_sof_pcm_platform_ack(struct snd_sof_dev *sdev,
483 struct snd_pcm_substream *substream)
485 if (sof_ops(sdev) && sof_ops(sdev)->pcm_ack)
486 return sof_ops(sdev)->pcm_ack(sdev, substream);
493 snd_sof_machine_register(struct snd_sof_dev *sdev, void *pdata)
495 if (sof_ops(sdev) && sof_ops(sdev)->machine_register)
496 return sof_ops(sdev)->machine_register(sdev, pdata);
502 snd_sof_machine_unregister(struct snd_sof_dev *sdev, void *pdata)
504 if (sof_ops(sdev) && sof_ops(sdev)->machine_unregister)
505 sof_ops(sdev)->machine_unregister(sdev, pdata);
508 static inline struct snd_soc_acpi_mach *
509 snd_sof_machine_select(struct snd_sof_dev *sdev)
511 if (sof_ops(sdev) && sof_ops(sdev)->machine_select)
512 return sof_ops(sdev)->machine_select(sdev);
518 snd_sof_set_mach_params(struct snd_soc_acpi_mach *mach,
519 struct snd_sof_dev *sdev)
521 if (sof_ops(sdev) && sof_ops(sdev)->set_mach_params)
522 sof_ops(sdev)->set_mach_params(mach, sdev);
526 * snd_sof_dsp_register_poll_timeout - Periodically poll an address
527 * until a condition is met or a timeout occurs
528 * @op: accessor function (takes @addr as its only argument)
529 * @addr: Address to poll
530 * @val: Variable to read the value into
531 * @cond: Break condition (usually involving @val)
532 * @sleep_us: Maximum time to sleep between reads in us (0
533 * tight-loops). Should be less than ~20ms since usleep_range
534 * is used (see Documentation/timers/timers-howto.rst).
535 * @timeout_us: Timeout in us, 0 means never timeout
537 * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
538 * case, the last read value at @addr is stored in @val. Must not
539 * be called from atomic context if sleep_us or timeout_us are used.
541 * This is modelled after the readx_poll_timeout macros in linux/iopoll.h.
543 #define snd_sof_dsp_read_poll_timeout(sdev, bar, offset, val, cond, sleep_us, timeout_us) \
545 u64 __timeout_us = (timeout_us); \
546 unsigned long __sleep_us = (sleep_us); \
547 ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
548 might_sleep_if((__sleep_us) != 0); \
550 (val) = snd_sof_dsp_read(sdev, bar, offset); \
553 "FW Poll Status: reg[%#x]=%#x successful\n", \
557 if (__timeout_us && \
558 ktime_compare(ktime_get(), __timeout) > 0) { \
559 (val) = snd_sof_dsp_read(sdev, bar, offset); \
561 "FW Poll Status: reg[%#x]=%#x timedout\n", \
566 usleep_range((__sleep_us >> 2) + 1, __sleep_us); \
568 (cond) ? 0 : -ETIMEDOUT; \
571 /* This is for registers bits with attribute RWC */
572 bool snd_sof_pci_update_bits(struct snd_sof_dev *sdev, u32 offset,
573 u32 mask, u32 value);
575 bool snd_sof_dsp_update_bits_unlocked(struct snd_sof_dev *sdev, u32 bar,
576 u32 offset, u32 mask, u32 value);
578 bool snd_sof_dsp_update_bits64_unlocked(struct snd_sof_dev *sdev, u32 bar,
579 u32 offset, u64 mask, u64 value);
581 bool snd_sof_dsp_update_bits(struct snd_sof_dev *sdev, u32 bar, u32 offset,
582 u32 mask, u32 value);
584 bool snd_sof_dsp_update_bits64(struct snd_sof_dev *sdev, u32 bar,
585 u32 offset, u64 mask, u64 value);
587 void snd_sof_dsp_update_bits_forced(struct snd_sof_dev *sdev, u32 bar,
588 u32 offset, u32 mask, u32 value);
590 int snd_sof_dsp_register_poll(struct snd_sof_dev *sdev, u32 bar, u32 offset,
591 u32 mask, u32 target, u32 timeout_ms,
594 void snd_sof_dsp_panic(struct snd_sof_dev *sdev, u32 offset, bool non_recoverable);