1e149ca29SPierre-Louis Bossart /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) */ 2d1d95fcbSLiam Girdwood /* 3d1d95fcbSLiam Girdwood * This file is provided under a dual BSD/GPLv2 license. When using or 4d1d95fcbSLiam Girdwood * redistributing this file, you may do so under either license. 5d1d95fcbSLiam Girdwood * 6d1d95fcbSLiam Girdwood * Copyright(c) 2018 Intel Corporation. All rights reserved. 7d1d95fcbSLiam Girdwood * 8d1d95fcbSLiam Girdwood * Author: Liam Girdwood <liam.r.girdwood@linux.intel.com> 9d1d95fcbSLiam Girdwood */ 10d1d95fcbSLiam Girdwood 11d1d95fcbSLiam Girdwood #ifndef __SOUND_SOC_SOF_IO_H 12d1d95fcbSLiam Girdwood #define __SOUND_SOC_SOF_IO_H 13d1d95fcbSLiam Girdwood 14d1d95fcbSLiam Girdwood #include <linux/device.h> 15d1d95fcbSLiam Girdwood #include <linux/interrupt.h> 16d1d95fcbSLiam Girdwood #include <linux/kernel.h> 17d1d95fcbSLiam Girdwood #include <linux/types.h> 18d1d95fcbSLiam Girdwood #include <sound/pcm.h> 19d1d95fcbSLiam Girdwood #include "sof-priv.h" 20d1d95fcbSLiam Girdwood 21d1d95fcbSLiam Girdwood #define sof_ops(sdev) \ 22d1d95fcbSLiam Girdwood ((sdev)->pdata->desc->ops) 23d1d95fcbSLiam Girdwood 24d1d95fcbSLiam Girdwood /* Mandatory operations are verified during probing */ 25d1d95fcbSLiam Girdwood 26d1d95fcbSLiam Girdwood /* init */ 27d1d95fcbSLiam Girdwood static inline int snd_sof_probe(struct snd_sof_dev *sdev) 28d1d95fcbSLiam Girdwood { 29d1d95fcbSLiam Girdwood return sof_ops(sdev)->probe(sdev); 30d1d95fcbSLiam Girdwood } 31d1d95fcbSLiam Girdwood 32d1d95fcbSLiam Girdwood static inline int snd_sof_remove(struct snd_sof_dev *sdev) 33d1d95fcbSLiam Girdwood { 34d1d95fcbSLiam Girdwood if (sof_ops(sdev)->remove) 35d1d95fcbSLiam Girdwood return sof_ops(sdev)->remove(sdev); 36d1d95fcbSLiam Girdwood 37d1d95fcbSLiam Girdwood return 0; 38d1d95fcbSLiam Girdwood } 39d1d95fcbSLiam Girdwood 407edb3051SKeyon Jie static inline int snd_sof_shutdown(struct snd_sof_dev *sdev) 417edb3051SKeyon Jie { 427edb3051SKeyon Jie if (sof_ops(sdev)->shutdown) 437edb3051SKeyon Jie return sof_ops(sdev)->shutdown(sdev); 447edb3051SKeyon Jie 457edb3051SKeyon Jie return 0; 467edb3051SKeyon Jie } 477edb3051SKeyon Jie 48d1d95fcbSLiam Girdwood /* control */ 49d1d95fcbSLiam Girdwood 50d1d95fcbSLiam Girdwood /* 51d1d95fcbSLiam Girdwood * snd_sof_dsp_run returns the core mask of the cores that are available 52d1d95fcbSLiam Girdwood * after successful fw boot 53d1d95fcbSLiam Girdwood */ 54d1d95fcbSLiam Girdwood static inline int snd_sof_dsp_run(struct snd_sof_dev *sdev) 55d1d95fcbSLiam Girdwood { 56d1d95fcbSLiam Girdwood return sof_ops(sdev)->run(sdev); 57d1d95fcbSLiam Girdwood } 58d1d95fcbSLiam Girdwood 59a70eb708SFred Oh static inline int snd_sof_dsp_stall(struct snd_sof_dev *sdev, unsigned int core_mask) 60d1d95fcbSLiam Girdwood { 61d1d95fcbSLiam Girdwood if (sof_ops(sdev)->stall) 62a70eb708SFred Oh return sof_ops(sdev)->stall(sdev, core_mask); 63d1d95fcbSLiam Girdwood 64d1d95fcbSLiam Girdwood return 0; 65d1d95fcbSLiam Girdwood } 66d1d95fcbSLiam Girdwood 67d1d95fcbSLiam Girdwood static inline int snd_sof_dsp_reset(struct snd_sof_dev *sdev) 68d1d95fcbSLiam Girdwood { 69d1d95fcbSLiam Girdwood if (sof_ops(sdev)->reset) 70d1d95fcbSLiam Girdwood return sof_ops(sdev)->reset(sdev); 71d1d95fcbSLiam Girdwood 72d1d95fcbSLiam Girdwood return 0; 73d1d95fcbSLiam Girdwood } 74d1d95fcbSLiam Girdwood 759ea80748SRanjani Sridharan /* dsp core get/put */ 76c414d5dfSRanjani Sridharan static inline int snd_sof_dsp_core_get(struct snd_sof_dev *sdev, int core) 77c414d5dfSRanjani Sridharan { 78c414d5dfSRanjani Sridharan if (core > sdev->num_cores - 1) { 79c414d5dfSRanjani Sridharan dev_err(sdev->dev, "invalid core id: %d for num_cores: %d\n", core, 80c414d5dfSRanjani Sridharan sdev->num_cores); 81c414d5dfSRanjani Sridharan return -EINVAL; 82c414d5dfSRanjani Sridharan } 83c414d5dfSRanjani Sridharan 84c414d5dfSRanjani Sridharan if (sof_ops(sdev)->core_get) { 85c414d5dfSRanjani Sridharan int ret; 86c414d5dfSRanjani Sridharan 87c414d5dfSRanjani Sridharan /* if current ref_count is > 0, increment it and return */ 88c414d5dfSRanjani Sridharan if (sdev->dsp_core_ref_count[core] > 0) { 89c414d5dfSRanjani Sridharan sdev->dsp_core_ref_count[core]++; 90c414d5dfSRanjani Sridharan return 0; 91c414d5dfSRanjani Sridharan } 92c414d5dfSRanjani Sridharan 93c414d5dfSRanjani Sridharan /* power up the core */ 94c414d5dfSRanjani Sridharan ret = sof_ops(sdev)->core_get(sdev, core); 95c414d5dfSRanjani Sridharan if (ret < 0) 96c414d5dfSRanjani Sridharan return ret; 97c414d5dfSRanjani Sridharan 98c414d5dfSRanjani Sridharan /* increment ref_count */ 99c414d5dfSRanjani Sridharan sdev->dsp_core_ref_count[core]++; 100c414d5dfSRanjani Sridharan 101c414d5dfSRanjani Sridharan /* and update enabled_cores_mask */ 102c414d5dfSRanjani Sridharan sdev->enabled_cores_mask |= BIT(core); 103c414d5dfSRanjani Sridharan 104c414d5dfSRanjani Sridharan dev_dbg(sdev->dev, "Core %d powered up\n", core); 105c414d5dfSRanjani Sridharan } 106c414d5dfSRanjani Sridharan 107c414d5dfSRanjani Sridharan return 0; 108c414d5dfSRanjani Sridharan } 109c414d5dfSRanjani Sridharan 110c414d5dfSRanjani Sridharan static inline int snd_sof_dsp_core_put(struct snd_sof_dev *sdev, int core) 111c414d5dfSRanjani Sridharan { 112c414d5dfSRanjani Sridharan if (core > sdev->num_cores - 1) { 113c414d5dfSRanjani Sridharan dev_err(sdev->dev, "invalid core id: %d for num_cores: %d\n", core, 114c414d5dfSRanjani Sridharan sdev->num_cores); 115c414d5dfSRanjani Sridharan return -EINVAL; 116c414d5dfSRanjani Sridharan } 117c414d5dfSRanjani Sridharan 118c414d5dfSRanjani Sridharan if (sof_ops(sdev)->core_put) { 119c414d5dfSRanjani Sridharan int ret; 120c414d5dfSRanjani Sridharan 121c414d5dfSRanjani Sridharan /* decrement ref_count and return if it is > 0 */ 122c414d5dfSRanjani Sridharan if (--(sdev->dsp_core_ref_count[core]) > 0) 123c414d5dfSRanjani Sridharan return 0; 124c414d5dfSRanjani Sridharan 125c414d5dfSRanjani Sridharan /* power down the core */ 126c414d5dfSRanjani Sridharan ret = sof_ops(sdev)->core_put(sdev, core); 127c414d5dfSRanjani Sridharan if (ret < 0) 128c414d5dfSRanjani Sridharan return ret; 129c414d5dfSRanjani Sridharan 130c414d5dfSRanjani Sridharan /* and update enabled_cores_mask */ 131c414d5dfSRanjani Sridharan sdev->enabled_cores_mask &= ~BIT(core); 132c414d5dfSRanjani Sridharan 133c414d5dfSRanjani Sridharan dev_dbg(sdev->dev, "Core %d powered down\n", core); 134c414d5dfSRanjani Sridharan } 135c414d5dfSRanjani Sridharan 136c414d5dfSRanjani Sridharan return 0; 137c414d5dfSRanjani Sridharan } 138c414d5dfSRanjani Sridharan 139d1d95fcbSLiam Girdwood /* pre/post fw load */ 140d1d95fcbSLiam Girdwood static inline int snd_sof_dsp_pre_fw_run(struct snd_sof_dev *sdev) 141d1d95fcbSLiam Girdwood { 142d1d95fcbSLiam Girdwood if (sof_ops(sdev)->pre_fw_run) 143d1d95fcbSLiam Girdwood return sof_ops(sdev)->pre_fw_run(sdev); 144d1d95fcbSLiam Girdwood 145d1d95fcbSLiam Girdwood return 0; 146d1d95fcbSLiam Girdwood } 147d1d95fcbSLiam Girdwood 148d1d95fcbSLiam Girdwood static inline int snd_sof_dsp_post_fw_run(struct snd_sof_dev *sdev) 149d1d95fcbSLiam Girdwood { 150d1d95fcbSLiam Girdwood if (sof_ops(sdev)->post_fw_run) 151d1d95fcbSLiam Girdwood return sof_ops(sdev)->post_fw_run(sdev); 152d1d95fcbSLiam Girdwood 153d1d95fcbSLiam Girdwood return 0; 154d1d95fcbSLiam Girdwood } 155d1d95fcbSLiam Girdwood 156e984f3efSFred Oh /* parse platform specific extended manifest */ 157e984f3efSFred Oh static inline int snd_sof_dsp_parse_platform_ext_manifest(struct snd_sof_dev *sdev, 158e984f3efSFred Oh const struct sof_ext_man_elem_header *hdr) 159e984f3efSFred Oh { 160e984f3efSFred Oh if (sof_ops(sdev)->parse_platform_ext_manifest) 161e984f3efSFred Oh return sof_ops(sdev)->parse_platform_ext_manifest(sdev, hdr); 162e984f3efSFred Oh 163e984f3efSFred Oh return 0; 164e984f3efSFred Oh } 165e984f3efSFred Oh 166ce8234a6SDaniel Baluta /* misc */ 167ce8234a6SDaniel Baluta 168ce8234a6SDaniel Baluta /** 169ce8234a6SDaniel Baluta * snd_sof_dsp_get_bar_index - Maps a section type with a BAR index 170ce8234a6SDaniel Baluta * 171ce8234a6SDaniel Baluta * @sdev: sof device 172ce8234a6SDaniel Baluta * @type: section type as described by snd_sof_fw_blk_type 173ce8234a6SDaniel Baluta * 174ce8234a6SDaniel Baluta * Returns the corresponding BAR index (a positive integer) or -EINVAL 175ce8234a6SDaniel Baluta * in case there is no mapping 176ce8234a6SDaniel Baluta */ 177ce8234a6SDaniel Baluta static inline int snd_sof_dsp_get_bar_index(struct snd_sof_dev *sdev, u32 type) 178ce8234a6SDaniel Baluta { 179ce8234a6SDaniel Baluta if (sof_ops(sdev)->get_bar_index) 180ce8234a6SDaniel Baluta return sof_ops(sdev)->get_bar_index(sdev, type); 181ce8234a6SDaniel Baluta 182ce8234a6SDaniel Baluta return sdev->mmio_bar; 183ce8234a6SDaniel Baluta } 184ce8234a6SDaniel Baluta 185bb9c93f5SDaniel Baluta static inline int snd_sof_dsp_get_mailbox_offset(struct snd_sof_dev *sdev) 186bb9c93f5SDaniel Baluta { 187bb9c93f5SDaniel Baluta if (sof_ops(sdev)->get_mailbox_offset) 188bb9c93f5SDaniel Baluta return sof_ops(sdev)->get_mailbox_offset(sdev); 189bb9c93f5SDaniel Baluta 190bb9c93f5SDaniel Baluta dev_err(sdev->dev, "error: %s not defined\n", __func__); 191bb9c93f5SDaniel Baluta return -ENOTSUPP; 192bb9c93f5SDaniel Baluta } 193bb9c93f5SDaniel Baluta 194e17422cdSDaniel Baluta static inline int snd_sof_dsp_get_window_offset(struct snd_sof_dev *sdev, 195e17422cdSDaniel Baluta u32 id) 196e17422cdSDaniel Baluta { 197e17422cdSDaniel Baluta if (sof_ops(sdev)->get_window_offset) 198e17422cdSDaniel Baluta return sof_ops(sdev)->get_window_offset(sdev, id); 199e17422cdSDaniel Baluta 200e17422cdSDaniel Baluta dev_err(sdev->dev, "error: %s not defined\n", __func__); 201e17422cdSDaniel Baluta return -ENOTSUPP; 202e17422cdSDaniel Baluta } 203d1d95fcbSLiam Girdwood /* power management */ 204d1d95fcbSLiam Girdwood static inline int snd_sof_dsp_resume(struct snd_sof_dev *sdev) 205d1d95fcbSLiam Girdwood { 206d1d95fcbSLiam Girdwood if (sof_ops(sdev)->resume) 207d1d95fcbSLiam Girdwood return sof_ops(sdev)->resume(sdev); 208d1d95fcbSLiam Girdwood 209d1d95fcbSLiam Girdwood return 0; 210d1d95fcbSLiam Girdwood } 211d1d95fcbSLiam Girdwood 21261e285caSRanjani Sridharan static inline int snd_sof_dsp_suspend(struct snd_sof_dev *sdev, 21361e285caSRanjani Sridharan u32 target_state) 214d1d95fcbSLiam Girdwood { 215d1d95fcbSLiam Girdwood if (sof_ops(sdev)->suspend) 21661e285caSRanjani Sridharan return sof_ops(sdev)->suspend(sdev, target_state); 217d1d95fcbSLiam Girdwood 218d1d95fcbSLiam Girdwood return 0; 219d1d95fcbSLiam Girdwood } 220d1d95fcbSLiam Girdwood 221d1d95fcbSLiam Girdwood static inline int snd_sof_dsp_runtime_resume(struct snd_sof_dev *sdev) 222d1d95fcbSLiam Girdwood { 223d1d95fcbSLiam Girdwood if (sof_ops(sdev)->runtime_resume) 224d1d95fcbSLiam Girdwood return sof_ops(sdev)->runtime_resume(sdev); 225d1d95fcbSLiam Girdwood 226d1d95fcbSLiam Girdwood return 0; 227d1d95fcbSLiam Girdwood } 228d1d95fcbSLiam Girdwood 2291c38c922SFred Oh static inline int snd_sof_dsp_runtime_suspend(struct snd_sof_dev *sdev) 230d1d95fcbSLiam Girdwood { 231d1d95fcbSLiam Girdwood if (sof_ops(sdev)->runtime_suspend) 2321c38c922SFred Oh return sof_ops(sdev)->runtime_suspend(sdev); 233d1d95fcbSLiam Girdwood 234d1d95fcbSLiam Girdwood return 0; 235d1d95fcbSLiam Girdwood } 236d1d95fcbSLiam Girdwood 23762fde977SKai Vehmanen static inline int snd_sof_dsp_runtime_idle(struct snd_sof_dev *sdev) 23862fde977SKai Vehmanen { 23962fde977SKai Vehmanen if (sof_ops(sdev)->runtime_idle) 24062fde977SKai Vehmanen return sof_ops(sdev)->runtime_idle(sdev); 24162fde977SKai Vehmanen 24262fde977SKai Vehmanen return 0; 24362fde977SKai Vehmanen } 24462fde977SKai Vehmanen 2457077a07aSRanjani Sridharan static inline int snd_sof_dsp_hw_params_upon_resume(struct snd_sof_dev *sdev) 246ed3baacdSRanjani Sridharan { 247ed3baacdSRanjani Sridharan if (sof_ops(sdev)->set_hw_params_upon_resume) 2487077a07aSRanjani Sridharan return sof_ops(sdev)->set_hw_params_upon_resume(sdev); 2497077a07aSRanjani Sridharan return 0; 250ed3baacdSRanjani Sridharan } 251ed3baacdSRanjani Sridharan 252d1d95fcbSLiam Girdwood static inline int snd_sof_dsp_set_clk(struct snd_sof_dev *sdev, u32 freq) 253d1d95fcbSLiam Girdwood { 254d1d95fcbSLiam Girdwood if (sof_ops(sdev)->set_clk) 255d1d95fcbSLiam Girdwood return sof_ops(sdev)->set_clk(sdev, freq); 256d1d95fcbSLiam Girdwood 257d1d95fcbSLiam Girdwood return 0; 258d1d95fcbSLiam Girdwood } 259d1d95fcbSLiam Girdwood 26061e285caSRanjani Sridharan static inline int 26161e285caSRanjani Sridharan snd_sof_dsp_set_power_state(struct snd_sof_dev *sdev, 26261e285caSRanjani Sridharan const struct sof_dsp_power_state *target_state) 263e8f112d8SKeyon Jie { 2648b66d7c5SKeyon Jie int ret = 0; 265e8f112d8SKeyon Jie 2668b66d7c5SKeyon Jie mutex_lock(&sdev->power_state_access); 2678b66d7c5SKeyon Jie 2688b66d7c5SKeyon Jie if (sof_ops(sdev)->set_power_state) 2698b66d7c5SKeyon Jie ret = sof_ops(sdev)->set_power_state(sdev, target_state); 2708b66d7c5SKeyon Jie 2718b66d7c5SKeyon Jie mutex_unlock(&sdev->power_state_access); 2728b66d7c5SKeyon Jie 2738b66d7c5SKeyon Jie return ret; 274e8f112d8SKeyon Jie } 275e8f112d8SKeyon Jie 276d1d95fcbSLiam Girdwood /* debug */ 2772f148430SPeter Ujfalusi void snd_sof_dsp_dbg_dump(struct snd_sof_dev *sdev, const char *msg, u32 flags); 2785e4a27fdSPan Xiuli 27907e833b4SPeter Ujfalusi static inline int snd_sof_debugfs_add_region_item(struct snd_sof_dev *sdev, 28007e833b4SPeter Ujfalusi enum snd_sof_fw_blk_type blk_type, u32 offset, size_t size, 28107e833b4SPeter Ujfalusi const char *name, enum sof_debugfs_access_type access_type) 28207e833b4SPeter Ujfalusi { 28307e833b4SPeter Ujfalusi if (sof_ops(sdev) && sof_ops(sdev)->debugfs_add_region_item) 28407e833b4SPeter Ujfalusi return sof_ops(sdev)->debugfs_add_region_item(sdev, blk_type, offset, 28507e833b4SPeter Ujfalusi size, name, access_type); 28607e833b4SPeter Ujfalusi 28707e833b4SPeter Ujfalusi return 0; 28807e833b4SPeter Ujfalusi } 28907e833b4SPeter Ujfalusi 290d1d95fcbSLiam Girdwood /* register IO */ 291d1d95fcbSLiam Girdwood static inline void snd_sof_dsp_write(struct snd_sof_dev *sdev, u32 bar, 292d1d95fcbSLiam Girdwood u32 offset, u32 value) 293d1d95fcbSLiam Girdwood { 294d1d95fcbSLiam Girdwood if (sof_ops(sdev)->write) { 295d1d95fcbSLiam Girdwood sof_ops(sdev)->write(sdev, sdev->bar[bar] + offset, value); 296d1d95fcbSLiam Girdwood return; 297d1d95fcbSLiam Girdwood } 298d1d95fcbSLiam Girdwood 299d1d95fcbSLiam Girdwood dev_err_ratelimited(sdev->dev, "error: %s not defined\n", __func__); 300d1d95fcbSLiam Girdwood } 301d1d95fcbSLiam Girdwood 302d1d95fcbSLiam Girdwood static inline void snd_sof_dsp_write64(struct snd_sof_dev *sdev, u32 bar, 303d1d95fcbSLiam Girdwood u32 offset, u64 value) 304d1d95fcbSLiam Girdwood { 305d1d95fcbSLiam Girdwood if (sof_ops(sdev)->write64) { 306d1d95fcbSLiam Girdwood sof_ops(sdev)->write64(sdev, sdev->bar[bar] + offset, value); 307d1d95fcbSLiam Girdwood return; 308d1d95fcbSLiam Girdwood } 309d1d95fcbSLiam Girdwood 310d1d95fcbSLiam Girdwood dev_err_ratelimited(sdev->dev, "error: %s not defined\n", __func__); 311d1d95fcbSLiam Girdwood } 312d1d95fcbSLiam Girdwood 313d1d95fcbSLiam Girdwood static inline u32 snd_sof_dsp_read(struct snd_sof_dev *sdev, u32 bar, 314d1d95fcbSLiam Girdwood u32 offset) 315d1d95fcbSLiam Girdwood { 316d1d95fcbSLiam Girdwood if (sof_ops(sdev)->read) 317d1d95fcbSLiam Girdwood return sof_ops(sdev)->read(sdev, sdev->bar[bar] + offset); 318d1d95fcbSLiam Girdwood 319d1d95fcbSLiam Girdwood dev_err(sdev->dev, "error: %s not defined\n", __func__); 320d1d95fcbSLiam Girdwood return -ENOTSUPP; 321d1d95fcbSLiam Girdwood } 322d1d95fcbSLiam Girdwood 323d1d95fcbSLiam Girdwood static inline u64 snd_sof_dsp_read64(struct snd_sof_dev *sdev, u32 bar, 324d1d95fcbSLiam Girdwood u32 offset) 325d1d95fcbSLiam Girdwood { 326d1d95fcbSLiam Girdwood if (sof_ops(sdev)->read64) 327d1d95fcbSLiam Girdwood return sof_ops(sdev)->read64(sdev, sdev->bar[bar] + offset); 328d1d95fcbSLiam Girdwood 329d1d95fcbSLiam Girdwood dev_err(sdev->dev, "error: %s not defined\n", __func__); 330d1d95fcbSLiam Girdwood return -ENOTSUPP; 331d1d95fcbSLiam Girdwood } 332d1d95fcbSLiam Girdwood 333d1d95fcbSLiam Girdwood /* block IO */ 3344624bb2fSPeter Ujfalusi static inline int snd_sof_dsp_block_read(struct snd_sof_dev *sdev, 3354624bb2fSPeter Ujfalusi enum snd_sof_fw_blk_type blk_type, 336d1d95fcbSLiam Girdwood u32 offset, void *dest, size_t bytes) 337d1d95fcbSLiam Girdwood { 3384624bb2fSPeter Ujfalusi return sof_ops(sdev)->block_read(sdev, blk_type, offset, dest, bytes); 339d1d95fcbSLiam Girdwood } 340d1d95fcbSLiam Girdwood 3414624bb2fSPeter Ujfalusi static inline int snd_sof_dsp_block_write(struct snd_sof_dev *sdev, 3424624bb2fSPeter Ujfalusi enum snd_sof_fw_blk_type blk_type, 343d1d95fcbSLiam Girdwood u32 offset, void *src, size_t bytes) 344d1d95fcbSLiam Girdwood { 3454624bb2fSPeter Ujfalusi return sof_ops(sdev)->block_write(sdev, blk_type, offset, src, bytes); 346d1d95fcbSLiam Girdwood } 347d1d95fcbSLiam Girdwood 348f71f59ddSDaniel Baluta /* mailbox IO */ 349f71f59ddSDaniel Baluta static inline void snd_sof_dsp_mailbox_read(struct snd_sof_dev *sdev, 350f71f59ddSDaniel Baluta u32 offset, void *dest, size_t bytes) 351f71f59ddSDaniel Baluta { 352f71f59ddSDaniel Baluta if (sof_ops(sdev)->mailbox_read) 353f71f59ddSDaniel Baluta sof_ops(sdev)->mailbox_read(sdev, offset, dest, bytes); 354f71f59ddSDaniel Baluta } 355f71f59ddSDaniel Baluta 356f71f59ddSDaniel Baluta static inline void snd_sof_dsp_mailbox_write(struct snd_sof_dev *sdev, 357f71f59ddSDaniel Baluta u32 offset, void *src, size_t bytes) 358f71f59ddSDaniel Baluta { 359f71f59ddSDaniel Baluta if (sof_ops(sdev)->mailbox_write) 360f71f59ddSDaniel Baluta sof_ops(sdev)->mailbox_write(sdev, offset, src, bytes); 361f71f59ddSDaniel Baluta } 362f71f59ddSDaniel Baluta 363d1d95fcbSLiam Girdwood /* ipc */ 364d1d95fcbSLiam Girdwood static inline int snd_sof_dsp_send_msg(struct snd_sof_dev *sdev, 365d1d95fcbSLiam Girdwood struct snd_sof_ipc_msg *msg) 366d1d95fcbSLiam Girdwood { 367d1d95fcbSLiam Girdwood return sof_ops(sdev)->send_msg(sdev, msg); 368d1d95fcbSLiam Girdwood } 369d1d95fcbSLiam Girdwood 370d1d95fcbSLiam Girdwood /* host DMA trace */ 371d1d95fcbSLiam Girdwood static inline int snd_sof_dma_trace_init(struct snd_sof_dev *sdev, 372bab05b50SPeter Ujfalusi struct sof_ipc_dma_trace_params_ext *dtrace_params) 373d1d95fcbSLiam Girdwood { 374d1d95fcbSLiam Girdwood if (sof_ops(sdev)->trace_init) 375bab05b50SPeter Ujfalusi return sof_ops(sdev)->trace_init(sdev, dtrace_params); 376d1d95fcbSLiam Girdwood 377d1d95fcbSLiam Girdwood return 0; 378d1d95fcbSLiam Girdwood } 379d1d95fcbSLiam Girdwood 380d1d95fcbSLiam Girdwood static inline int snd_sof_dma_trace_release(struct snd_sof_dev *sdev) 381d1d95fcbSLiam Girdwood { 382d1d95fcbSLiam Girdwood if (sof_ops(sdev)->trace_release) 383d1d95fcbSLiam Girdwood return sof_ops(sdev)->trace_release(sdev); 384d1d95fcbSLiam Girdwood 385d1d95fcbSLiam Girdwood return 0; 386d1d95fcbSLiam Girdwood } 387d1d95fcbSLiam Girdwood 388d1d95fcbSLiam Girdwood static inline int snd_sof_dma_trace_trigger(struct snd_sof_dev *sdev, int cmd) 389d1d95fcbSLiam Girdwood { 390d1d95fcbSLiam Girdwood if (sof_ops(sdev)->trace_trigger) 391d1d95fcbSLiam Girdwood return sof_ops(sdev)->trace_trigger(sdev, cmd); 392d1d95fcbSLiam Girdwood 393d1d95fcbSLiam Girdwood return 0; 394d1d95fcbSLiam Girdwood } 395d1d95fcbSLiam Girdwood 396d1d95fcbSLiam Girdwood /* host PCM ops */ 397d1d95fcbSLiam Girdwood static inline int 398d1d95fcbSLiam Girdwood snd_sof_pcm_platform_open(struct snd_sof_dev *sdev, 399d1d95fcbSLiam Girdwood struct snd_pcm_substream *substream) 400d1d95fcbSLiam Girdwood { 401d1d95fcbSLiam Girdwood if (sof_ops(sdev) && sof_ops(sdev)->pcm_open) 402d1d95fcbSLiam Girdwood return sof_ops(sdev)->pcm_open(sdev, substream); 403d1d95fcbSLiam Girdwood 404d1d95fcbSLiam Girdwood return 0; 405d1d95fcbSLiam Girdwood } 406d1d95fcbSLiam Girdwood 407d1d95fcbSLiam Girdwood /* disconnect pcm substream to a host stream */ 408d1d95fcbSLiam Girdwood static inline int 409d1d95fcbSLiam Girdwood snd_sof_pcm_platform_close(struct snd_sof_dev *sdev, 410d1d95fcbSLiam Girdwood struct snd_pcm_substream *substream) 411d1d95fcbSLiam Girdwood { 412d1d95fcbSLiam Girdwood if (sof_ops(sdev) && sof_ops(sdev)->pcm_close) 413d1d95fcbSLiam Girdwood return sof_ops(sdev)->pcm_close(sdev, substream); 414d1d95fcbSLiam Girdwood 415d1d95fcbSLiam Girdwood return 0; 416d1d95fcbSLiam Girdwood } 417d1d95fcbSLiam Girdwood 418d1d95fcbSLiam Girdwood /* host stream hw params */ 419d1d95fcbSLiam Girdwood static inline int 420d1d95fcbSLiam Girdwood snd_sof_pcm_platform_hw_params(struct snd_sof_dev *sdev, 421d1d95fcbSLiam Girdwood struct snd_pcm_substream *substream, 422d1d95fcbSLiam Girdwood struct snd_pcm_hw_params *params, 42331f60a0cSPeter Ujfalusi struct snd_sof_platform_stream_params *platform_params) 424d1d95fcbSLiam Girdwood { 425d1d95fcbSLiam Girdwood if (sof_ops(sdev) && sof_ops(sdev)->pcm_hw_params) 42631f60a0cSPeter Ujfalusi return sof_ops(sdev)->pcm_hw_params(sdev, substream, params, 42731f60a0cSPeter Ujfalusi platform_params); 428d1d95fcbSLiam Girdwood 429d1d95fcbSLiam Girdwood return 0; 430d1d95fcbSLiam Girdwood } 431d1d95fcbSLiam Girdwood 43293146bc2SRanjani Sridharan /* host stream hw free */ 43393146bc2SRanjani Sridharan static inline int 43493146bc2SRanjani Sridharan snd_sof_pcm_platform_hw_free(struct snd_sof_dev *sdev, 43593146bc2SRanjani Sridharan struct snd_pcm_substream *substream) 43693146bc2SRanjani Sridharan { 43793146bc2SRanjani Sridharan if (sof_ops(sdev) && sof_ops(sdev)->pcm_hw_free) 43893146bc2SRanjani Sridharan return sof_ops(sdev)->pcm_hw_free(sdev, substream); 43993146bc2SRanjani Sridharan 44093146bc2SRanjani Sridharan return 0; 44193146bc2SRanjani Sridharan } 44293146bc2SRanjani Sridharan 443d1d95fcbSLiam Girdwood /* host stream trigger */ 444d1d95fcbSLiam Girdwood static inline int 445d1d95fcbSLiam Girdwood snd_sof_pcm_platform_trigger(struct snd_sof_dev *sdev, 446d1d95fcbSLiam Girdwood struct snd_pcm_substream *substream, int cmd) 447d1d95fcbSLiam Girdwood { 448d1d95fcbSLiam Girdwood if (sof_ops(sdev) && sof_ops(sdev)->pcm_trigger) 449d1d95fcbSLiam Girdwood return sof_ops(sdev)->pcm_trigger(sdev, substream, cmd); 450d1d95fcbSLiam Girdwood 451d1d95fcbSLiam Girdwood return 0; 452d1d95fcbSLiam Girdwood } 453d1d95fcbSLiam Girdwood 45496ec1741SPeter Ujfalusi /* Firmware loading */ 45596ec1741SPeter Ujfalusi static inline int snd_sof_load_firmware(struct snd_sof_dev *sdev) 45696ec1741SPeter Ujfalusi { 45796ec1741SPeter Ujfalusi dev_dbg(sdev->dev, "loading firmware\n"); 45896ec1741SPeter Ujfalusi 45996ec1741SPeter Ujfalusi return sof_ops(sdev)->load_firmware(sdev); 46096ec1741SPeter Ujfalusi } 46196ec1741SPeter Ujfalusi 462d1d95fcbSLiam Girdwood /* host DSP message data */ 4636a0ba071SGuennadi Liakhovetski static inline int snd_sof_ipc_msg_data(struct snd_sof_dev *sdev, 464d1d95fcbSLiam Girdwood struct snd_pcm_substream *substream, 465d1d95fcbSLiam Girdwood void *p, size_t sz) 466d1d95fcbSLiam Girdwood { 4676a0ba071SGuennadi Liakhovetski return sof_ops(sdev)->ipc_msg_data(sdev, substream, p, sz); 468d1d95fcbSLiam Girdwood } 469d1d95fcbSLiam Girdwood 470d1d95fcbSLiam Girdwood /* host configure DSP HW parameters */ 471d1d95fcbSLiam Girdwood static inline int 472d1d95fcbSLiam Girdwood snd_sof_ipc_pcm_params(struct snd_sof_dev *sdev, 473d1d95fcbSLiam Girdwood struct snd_pcm_substream *substream, 474d1d95fcbSLiam Girdwood const struct sof_ipc_pcm_params_reply *reply) 475d1d95fcbSLiam Girdwood { 476d1d95fcbSLiam Girdwood return sof_ops(sdev)->ipc_pcm_params(sdev, substream, reply); 477d1d95fcbSLiam Girdwood } 478d1d95fcbSLiam Girdwood 479*757ce810SPeter Ujfalusi /* host side configuration of the stream's data offset in stream mailbox area */ 480*757ce810SPeter Ujfalusi static inline int 481*757ce810SPeter Ujfalusi snd_sof_set_stream_data_offset(struct snd_sof_dev *sdev, 482*757ce810SPeter Ujfalusi struct snd_pcm_substream *substream, 483*757ce810SPeter Ujfalusi size_t posn_offset) 484*757ce810SPeter Ujfalusi { 485*757ce810SPeter Ujfalusi if (sof_ops(sdev) && sof_ops(sdev)->set_stream_data_offset) 486*757ce810SPeter Ujfalusi return sof_ops(sdev)->set_stream_data_offset(sdev, substream, 487*757ce810SPeter Ujfalusi posn_offset); 488*757ce810SPeter Ujfalusi 489*757ce810SPeter Ujfalusi return 0; 490*757ce810SPeter Ujfalusi } 491*757ce810SPeter Ujfalusi 492d1d95fcbSLiam Girdwood /* host stream pointer */ 493d1d95fcbSLiam Girdwood static inline snd_pcm_uframes_t 494d1d95fcbSLiam Girdwood snd_sof_pcm_platform_pointer(struct snd_sof_dev *sdev, 495d1d95fcbSLiam Girdwood struct snd_pcm_substream *substream) 496d1d95fcbSLiam Girdwood { 497d1d95fcbSLiam Girdwood if (sof_ops(sdev) && sof_ops(sdev)->pcm_pointer) 498d1d95fcbSLiam Girdwood return sof_ops(sdev)->pcm_pointer(sdev, substream); 499d1d95fcbSLiam Girdwood 500d1d95fcbSLiam Girdwood return 0; 501d1d95fcbSLiam Girdwood } 502d1d95fcbSLiam Girdwood 5034a39ea3fSRanjani Sridharan /* pcm ack */ 5044a39ea3fSRanjani Sridharan static inline int snd_sof_pcm_platform_ack(struct snd_sof_dev *sdev, 5054a39ea3fSRanjani Sridharan struct snd_pcm_substream *substream) 5064a39ea3fSRanjani Sridharan { 5074a39ea3fSRanjani Sridharan if (sof_ops(sdev) && sof_ops(sdev)->pcm_ack) 5084a39ea3fSRanjani Sridharan return sof_ops(sdev)->pcm_ack(sdev, substream); 5094a39ea3fSRanjani Sridharan 5104a39ea3fSRanjani Sridharan return 0; 5114a39ea3fSRanjani Sridharan } 5124a39ea3fSRanjani Sridharan 513285880a2SDaniel Baluta /* machine driver */ 514285880a2SDaniel Baluta static inline int 515285880a2SDaniel Baluta snd_sof_machine_register(struct snd_sof_dev *sdev, void *pdata) 516285880a2SDaniel Baluta { 517285880a2SDaniel Baluta if (sof_ops(sdev) && sof_ops(sdev)->machine_register) 518285880a2SDaniel Baluta return sof_ops(sdev)->machine_register(sdev, pdata); 519285880a2SDaniel Baluta 520285880a2SDaniel Baluta return 0; 521285880a2SDaniel Baluta } 522285880a2SDaniel Baluta 523285880a2SDaniel Baluta static inline void 524285880a2SDaniel Baluta snd_sof_machine_unregister(struct snd_sof_dev *sdev, void *pdata) 525285880a2SDaniel Baluta { 526285880a2SDaniel Baluta if (sof_ops(sdev) && sof_ops(sdev)->machine_unregister) 527285880a2SDaniel Baluta sof_ops(sdev)->machine_unregister(sdev, pdata); 528285880a2SDaniel Baluta } 529285880a2SDaniel Baluta 530cb515f10SGuennadi Liakhovetski static inline struct snd_soc_acpi_mach * 531285880a2SDaniel Baluta snd_sof_machine_select(struct snd_sof_dev *sdev) 532285880a2SDaniel Baluta { 533285880a2SDaniel Baluta if (sof_ops(sdev) && sof_ops(sdev)->machine_select) 534cb515f10SGuennadi Liakhovetski return sof_ops(sdev)->machine_select(sdev); 535cb515f10SGuennadi Liakhovetski 536cb515f10SGuennadi Liakhovetski return NULL; 537285880a2SDaniel Baluta } 538285880a2SDaniel Baluta 539285880a2SDaniel Baluta static inline void 540cb515f10SGuennadi Liakhovetski snd_sof_set_mach_params(struct snd_soc_acpi_mach *mach, 54117e9d6b0SPierre-Louis Bossart struct snd_sof_dev *sdev) 542285880a2SDaniel Baluta { 543285880a2SDaniel Baluta if (sof_ops(sdev) && sof_ops(sdev)->set_mach_params) 54417e9d6b0SPierre-Louis Bossart sof_ops(sdev)->set_mach_params(mach, sdev); 545285880a2SDaniel Baluta } 546285880a2SDaniel Baluta 547d1d95fcbSLiam Girdwood /** 548d1d95fcbSLiam Girdwood * snd_sof_dsp_register_poll_timeout - Periodically poll an address 549d1d95fcbSLiam Girdwood * until a condition is met or a timeout occurs 550d1d95fcbSLiam Girdwood * @op: accessor function (takes @addr as its only argument) 551d1d95fcbSLiam Girdwood * @addr: Address to poll 552d1d95fcbSLiam Girdwood * @val: Variable to read the value into 553d1d95fcbSLiam Girdwood * @cond: Break condition (usually involving @val) 554d1d95fcbSLiam Girdwood * @sleep_us: Maximum time to sleep between reads in us (0 555d1d95fcbSLiam Girdwood * tight-loops). Should be less than ~20ms since usleep_range 556458f69efSMauro Carvalho Chehab * is used (see Documentation/timers/timers-howto.rst). 557d1d95fcbSLiam Girdwood * @timeout_us: Timeout in us, 0 means never timeout 558d1d95fcbSLiam Girdwood * 559d1d95fcbSLiam Girdwood * Returns 0 on success and -ETIMEDOUT upon a timeout. In either 560d1d95fcbSLiam Girdwood * case, the last read value at @addr is stored in @val. Must not 561d1d95fcbSLiam Girdwood * be called from atomic context if sleep_us or timeout_us are used. 562d1d95fcbSLiam Girdwood * 563d1d95fcbSLiam Girdwood * This is modelled after the readx_poll_timeout macros in linux/iopoll.h. 564d1d95fcbSLiam Girdwood */ 565d1d95fcbSLiam Girdwood #define snd_sof_dsp_read_poll_timeout(sdev, bar, offset, val, cond, sleep_us, timeout_us) \ 566d1d95fcbSLiam Girdwood ({ \ 567d1d95fcbSLiam Girdwood u64 __timeout_us = (timeout_us); \ 568d1d95fcbSLiam Girdwood unsigned long __sleep_us = (sleep_us); \ 569d1d95fcbSLiam Girdwood ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \ 570d1d95fcbSLiam Girdwood might_sleep_if((__sleep_us) != 0); \ 571d1d95fcbSLiam Girdwood for (;;) { \ 572d1d95fcbSLiam Girdwood (val) = snd_sof_dsp_read(sdev, bar, offset); \ 573d1d95fcbSLiam Girdwood if (cond) { \ 574d1d95fcbSLiam Girdwood dev_dbg(sdev->dev, \ 5753b2e93edSKeyon Jie "FW Poll Status: reg[%#x]=%#x successful\n", \ 5763b2e93edSKeyon Jie (offset), (val)); \ 577d1d95fcbSLiam Girdwood break; \ 578d1d95fcbSLiam Girdwood } \ 579d1d95fcbSLiam Girdwood if (__timeout_us && \ 580d1d95fcbSLiam Girdwood ktime_compare(ktime_get(), __timeout) > 0) { \ 581d1d95fcbSLiam Girdwood (val) = snd_sof_dsp_read(sdev, bar, offset); \ 582d1d95fcbSLiam Girdwood dev_dbg(sdev->dev, \ 5833b2e93edSKeyon Jie "FW Poll Status: reg[%#x]=%#x timedout\n", \ 5843b2e93edSKeyon Jie (offset), (val)); \ 585d1d95fcbSLiam Girdwood break; \ 586d1d95fcbSLiam Girdwood } \ 587d1d95fcbSLiam Girdwood if (__sleep_us) \ 588d1d95fcbSLiam Girdwood usleep_range((__sleep_us >> 2) + 1, __sleep_us); \ 589d1d95fcbSLiam Girdwood } \ 590d1d95fcbSLiam Girdwood (cond) ? 0 : -ETIMEDOUT; \ 591d1d95fcbSLiam Girdwood }) 592d1d95fcbSLiam Girdwood 593d1d95fcbSLiam Girdwood /* This is for registers bits with attribute RWC */ 594d1d95fcbSLiam Girdwood bool snd_sof_pci_update_bits(struct snd_sof_dev *sdev, u32 offset, 595d1d95fcbSLiam Girdwood u32 mask, u32 value); 596d1d95fcbSLiam Girdwood 597d1d95fcbSLiam Girdwood bool snd_sof_dsp_update_bits_unlocked(struct snd_sof_dev *sdev, u32 bar, 598d1d95fcbSLiam Girdwood u32 offset, u32 mask, u32 value); 599d1d95fcbSLiam Girdwood 600d1d95fcbSLiam Girdwood bool snd_sof_dsp_update_bits64_unlocked(struct snd_sof_dev *sdev, u32 bar, 601d1d95fcbSLiam Girdwood u32 offset, u64 mask, u64 value); 602d1d95fcbSLiam Girdwood 603d1d95fcbSLiam Girdwood bool snd_sof_dsp_update_bits(struct snd_sof_dev *sdev, u32 bar, u32 offset, 604d1d95fcbSLiam Girdwood u32 mask, u32 value); 605d1d95fcbSLiam Girdwood 606d1d95fcbSLiam Girdwood bool snd_sof_dsp_update_bits64(struct snd_sof_dev *sdev, u32 bar, 607d1d95fcbSLiam Girdwood u32 offset, u64 mask, u64 value); 608d1d95fcbSLiam Girdwood 609d1d95fcbSLiam Girdwood void snd_sof_dsp_update_bits_forced(struct snd_sof_dev *sdev, u32 bar, 610d1d95fcbSLiam Girdwood u32 offset, u32 mask, u32 value); 611d1d95fcbSLiam Girdwood 612d1d95fcbSLiam Girdwood int snd_sof_dsp_register_poll(struct snd_sof_dev *sdev, u32 bar, u32 offset, 613d1d95fcbSLiam Girdwood u32 mask, u32 target, u32 timeout_ms, 614d1d95fcbSLiam Girdwood u32 interval_us); 615d1d95fcbSLiam Girdwood 616b2b10aa7SPeter Ujfalusi void snd_sof_dsp_panic(struct snd_sof_dev *sdev, u32 offset, bool non_recoverable); 617d1d95fcbSLiam Girdwood #endif 618