diff options
| author | Jin Yao <yao.jin@linux.intel.com> | 2015-04-06 21:33:30 -0400 |
|---|---|---|
| committer | Mark Brown <broonie@kernel.org> | 2015-04-10 14:00:37 -0400 |
| commit | a33c1ec5cf82efb76f0e7339b13f11cfb53a2a2f (patch) | |
| tree | 55d65260a66476cf556ba71a58a14f77aea7ccef | |
| parent | 75afbd052b3675e9b812f9327e19be63f3e7b5de (diff) | |
ASoC: Intel: Refactor common IPC/mailbox code into generic APIs
Currently in Intel SST driver, some similar IPC/mailbox processing
code are used in different platforms (e.g. in baytrail/broadwell).
This patch extracts the common code and creates new files
(sst-ipc.c/sst-ipc.h) to contain the common code and provide the generic
APIs for IPC/mailbox processing.
Signed-off-by: Jin Yao <yao.jin@linux.intel.com>
Acked-by: Jie Yang <yang.jie@intel.com>
Signed-off-by: Mark Brown <broonie@kernel.org>
| -rw-r--r-- | sound/soc/intel/common/Makefile | 3 | ||||
| -rw-r--r-- | sound/soc/intel/common/sst-ipc.c | 294 | ||||
| -rw-r--r-- | sound/soc/intel/common/sst-ipc.h | 91 |
3 files changed, 387 insertions, 1 deletions
diff --git a/sound/soc/intel/common/Makefile b/sound/soc/intel/common/Makefile index 3df0e1ca76c0..f24154ca4e98 100644 --- a/sound/soc/intel/common/Makefile +++ b/sound/soc/intel/common/Makefile | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | snd-soc-sst-dsp-objs := sst-dsp.o sst-firmware.o | 1 | snd-soc-sst-dsp-objs := sst-dsp.o sst-firmware.o |
| 2 | snd-soc-sst-acpi-objs := sst-acpi.o | 2 | snd-soc-sst-acpi-objs := sst-acpi.o |
| 3 | snd-soc-sst-ipc-objs := sst-ipc.o | ||
| 3 | 4 | ||
| 4 | obj-$(CONFIG_SND_SOC_INTEL_SST) += snd-soc-sst-dsp.o | 5 | obj-$(CONFIG_SND_SOC_INTEL_SST) += snd-soc-sst-dsp.o snd-soc-sst-ipc.o |
| 5 | obj-$(CONFIG_SND_SOC_INTEL_SST_ACPI) += snd-soc-sst-acpi.o | 6 | obj-$(CONFIG_SND_SOC_INTEL_SST_ACPI) += snd-soc-sst-acpi.o |
| 6 | 7 | ||
diff --git a/sound/soc/intel/common/sst-ipc.c b/sound/soc/intel/common/sst-ipc.c new file mode 100644 index 000000000000..4b62a553823c --- /dev/null +++ b/sound/soc/intel/common/sst-ipc.c | |||
| @@ -0,0 +1,294 @@ | |||
| 1 | /* | ||
| 2 | * Intel SST generic IPC Support | ||
| 3 | * | ||
| 4 | * Copyright (C) 2015, Intel Corporation. All rights reserved. | ||
| 5 | * | ||
| 6 | * This program is free software; you can redistribute it and/or | ||
| 7 | * modify it under the terms of the GNU General Public License version | ||
| 8 | * 2 as published by the Free Software Foundation. | ||
| 9 | * | ||
| 10 | * This program is distributed in the hope that it will be useful, | ||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | * GNU General Public License for more details. | ||
| 14 | * | ||
| 15 | */ | ||
| 16 | |||
| 17 | #include <linux/types.h> | ||
| 18 | #include <linux/kernel.h> | ||
| 19 | #include <linux/list.h> | ||
| 20 | #include <linux/wait.h> | ||
| 21 | #include <linux/module.h> | ||
| 22 | #include <linux/spinlock.h> | ||
| 23 | #include <linux/device.h> | ||
| 24 | #include <linux/slab.h> | ||
| 25 | #include <linux/workqueue.h> | ||
| 26 | #include <linux/sched.h> | ||
| 27 | #include <linux/delay.h> | ||
| 28 | #include <linux/platform_device.h> | ||
| 29 | #include <linux/kthread.h> | ||
| 30 | #include <sound/asound.h> | ||
| 31 | |||
| 32 | #include "sst-dsp.h" | ||
| 33 | #include "sst-dsp-priv.h" | ||
| 34 | #include "sst-ipc.h" | ||
| 35 | |||
| 36 | /* IPC message timeout (msecs) */ | ||
| 37 | #define IPC_TIMEOUT_MSECS 300 | ||
| 38 | |||
| 39 | #define IPC_EMPTY_LIST_SIZE 8 | ||
| 40 | |||
| 41 | /* locks held by caller */ | ||
| 42 | static struct ipc_message *msg_get_empty(struct sst_generic_ipc *ipc) | ||
| 43 | { | ||
| 44 | struct ipc_message *msg = NULL; | ||
| 45 | |||
| 46 | if (!list_empty(&ipc->empty_list)) { | ||
| 47 | msg = list_first_entry(&ipc->empty_list, struct ipc_message, | ||
| 48 | list); | ||
| 49 | list_del(&msg->list); | ||
| 50 | } | ||
| 51 | |||
| 52 | return msg; | ||
| 53 | } | ||
| 54 | |||
| 55 | static int tx_wait_done(struct sst_generic_ipc *ipc, | ||
| 56 | struct ipc_message *msg, void *rx_data) | ||
| 57 | { | ||
| 58 | unsigned long flags; | ||
| 59 | int ret; | ||
| 60 | |||
| 61 | /* wait for DSP completion (in all cases atm inc pending) */ | ||
| 62 | ret = wait_event_timeout(msg->waitq, msg->complete, | ||
| 63 | msecs_to_jiffies(IPC_TIMEOUT_MSECS)); | ||
| 64 | |||
| 65 | spin_lock_irqsave(&ipc->dsp->spinlock, flags); | ||
| 66 | if (ret == 0) { | ||
| 67 | if (ipc->ops.shim_dbg != NULL) | ||
| 68 | ipc->ops.shim_dbg(ipc, "message timeout"); | ||
| 69 | |||
| 70 | list_del(&msg->list); | ||
| 71 | ret = -ETIMEDOUT; | ||
| 72 | } else { | ||
| 73 | |||
| 74 | /* copy the data returned from DSP */ | ||
| 75 | if (msg->rx_size) | ||
| 76 | memcpy(rx_data, msg->rx_data, msg->rx_size); | ||
| 77 | ret = msg->errno; | ||
| 78 | } | ||
| 79 | |||
| 80 | list_add_tail(&msg->list, &ipc->empty_list); | ||
| 81 | spin_unlock_irqrestore(&ipc->dsp->spinlock, flags); | ||
| 82 | return ret; | ||
| 83 | } | ||
| 84 | |||
| 85 | static int ipc_tx_message(struct sst_generic_ipc *ipc, u64 header, | ||
| 86 | void *tx_data, size_t tx_bytes, void *rx_data, | ||
| 87 | size_t rx_bytes, int wait) | ||
| 88 | { | ||
| 89 | struct ipc_message *msg; | ||
| 90 | unsigned long flags; | ||
| 91 | |||
| 92 | spin_lock_irqsave(&ipc->dsp->spinlock, flags); | ||
| 93 | |||
| 94 | msg = msg_get_empty(ipc); | ||
| 95 | if (msg == NULL) { | ||
| 96 | spin_unlock_irqrestore(&ipc->dsp->spinlock, flags); | ||
| 97 | return -EBUSY; | ||
| 98 | } | ||
| 99 | |||
| 100 | msg->header = header; | ||
| 101 | msg->tx_size = tx_bytes; | ||
| 102 | msg->rx_size = rx_bytes; | ||
| 103 | msg->wait = wait; | ||
| 104 | msg->errno = 0; | ||
| 105 | msg->pending = false; | ||
| 106 | msg->complete = false; | ||
| 107 | |||
| 108 | if ((tx_bytes) && (ipc->ops.tx_data_copy != NULL)) | ||
| 109 | ipc->ops.tx_data_copy(msg, tx_data, tx_bytes); | ||
| 110 | |||
| 111 | list_add_tail(&msg->list, &ipc->tx_list); | ||
| 112 | spin_unlock_irqrestore(&ipc->dsp->spinlock, flags); | ||
| 113 | |||
| 114 | queue_kthread_work(&ipc->kworker, &ipc->kwork); | ||
| 115 | |||
| 116 | if (wait) | ||
| 117 | return tx_wait_done(ipc, msg, rx_data); | ||
| 118 | else | ||
| 119 | return 0; | ||
| 120 | } | ||
| 121 | |||
| 122 | static int msg_empty_list_init(struct sst_generic_ipc *ipc) | ||
| 123 | { | ||
| 124 | int i; | ||
| 125 | |||
| 126 | ipc->msg = kzalloc(sizeof(struct ipc_message) * | ||
| 127 | IPC_EMPTY_LIST_SIZE, GFP_KERNEL); | ||
| 128 | if (ipc->msg == NULL) | ||
| 129 | return -ENOMEM; | ||
| 130 | |||
| 131 | for (i = 0; i < IPC_EMPTY_LIST_SIZE; i++) { | ||
| 132 | init_waitqueue_head(&ipc->msg[i].waitq); | ||
| 133 | list_add(&ipc->msg[i].list, &ipc->empty_list); | ||
| 134 | } | ||
| 135 | |||
| 136 | return 0; | ||
| 137 | } | ||
| 138 | |||
| 139 | static void ipc_tx_msgs(struct kthread_work *work) | ||
| 140 | { | ||
| 141 | struct sst_generic_ipc *ipc = | ||
| 142 | container_of(work, struct sst_generic_ipc, kwork); | ||
| 143 | struct ipc_message *msg; | ||
| 144 | unsigned long flags; | ||
| 145 | u64 ipcx; | ||
| 146 | |||
| 147 | spin_lock_irqsave(&ipc->dsp->spinlock, flags); | ||
| 148 | |||
| 149 | if (list_empty(&ipc->tx_list) || ipc->pending) { | ||
| 150 | spin_unlock_irqrestore(&ipc->dsp->spinlock, flags); | ||
| 151 | return; | ||
| 152 | } | ||
| 153 | |||
| 154 | /* if the DSP is busy, we will TX messages after IRQ. | ||
| 155 | * also postpone if we are in the middle of procesing completion irq*/ | ||
| 156 | ipcx = sst_dsp_shim_read_unlocked(ipc->dsp, SST_IPCX); | ||
| 157 | if (ipcx & (SST_IPCX_BUSY | SST_IPCX_DONE)) { | ||
| 158 | spin_unlock_irqrestore(&ipc->dsp->spinlock, flags); | ||
| 159 | return; | ||
| 160 | } | ||
| 161 | |||
| 162 | msg = list_first_entry(&ipc->tx_list, struct ipc_message, list); | ||
| 163 | list_move(&msg->list, &ipc->rx_list); | ||
| 164 | |||
| 165 | if (ipc->ops.tx_msg != NULL) | ||
| 166 | ipc->ops.tx_msg(ipc, msg); | ||
| 167 | |||
| 168 | spin_unlock_irqrestore(&ipc->dsp->spinlock, flags); | ||
| 169 | } | ||
| 170 | |||
| 171 | int sst_ipc_tx_message_wait(struct sst_generic_ipc *ipc, u64 header, | ||
| 172 | void *tx_data, size_t tx_bytes, void *rx_data, size_t rx_bytes) | ||
| 173 | { | ||
| 174 | return ipc_tx_message(ipc, header, tx_data, tx_bytes, | ||
| 175 | rx_data, rx_bytes, 1); | ||
| 176 | } | ||
| 177 | EXPORT_SYMBOL_GPL(sst_ipc_tx_message_wait); | ||
| 178 | |||
| 179 | int sst_ipc_tx_message_nowait(struct sst_generic_ipc *ipc, u64 header, | ||
| 180 | void *tx_data, size_t tx_bytes) | ||
| 181 | { | ||
| 182 | return ipc_tx_message(ipc, header, tx_data, tx_bytes, | ||
| 183 | NULL, 0, 0); | ||
| 184 | } | ||
| 185 | EXPORT_SYMBOL_GPL(sst_ipc_tx_message_nowait); | ||
| 186 | |||
| 187 | struct ipc_message *sst_ipc_reply_find_msg(struct sst_generic_ipc *ipc, | ||
| 188 | u64 header) | ||
| 189 | { | ||
| 190 | struct ipc_message *msg; | ||
| 191 | u64 mask; | ||
| 192 | |||
| 193 | if (ipc->ops.reply_msg_match != NULL) | ||
| 194 | header = ipc->ops.reply_msg_match(header, &mask); | ||
| 195 | |||
| 196 | if (list_empty(&ipc->rx_list)) { | ||
| 197 | dev_err(ipc->dev, "error: rx list empty but received 0x%llx\n", | ||
| 198 | header); | ||
| 199 | return NULL; | ||
| 200 | } | ||
| 201 | |||
| 202 | list_for_each_entry(msg, &ipc->rx_list, list) { | ||
| 203 | if ((msg->header & mask) == header) | ||
| 204 | return msg; | ||
| 205 | } | ||
| 206 | |||
| 207 | return NULL; | ||
| 208 | } | ||
| 209 | EXPORT_SYMBOL_GPL(sst_ipc_reply_find_msg); | ||
| 210 | |||
| 211 | /* locks held by caller */ | ||
| 212 | void sst_ipc_tx_msg_reply_complete(struct sst_generic_ipc *ipc, | ||
| 213 | struct ipc_message *msg) | ||
| 214 | { | ||
| 215 | msg->complete = true; | ||
| 216 | |||
| 217 | if (!msg->wait) | ||
| 218 | list_add_tail(&msg->list, &ipc->empty_list); | ||
| 219 | else | ||
| 220 | wake_up(&msg->waitq); | ||
| 221 | } | ||
| 222 | EXPORT_SYMBOL_GPL(sst_ipc_tx_msg_reply_complete); | ||
| 223 | |||
| 224 | void sst_ipc_drop_all(struct sst_generic_ipc *ipc) | ||
| 225 | { | ||
| 226 | struct ipc_message *msg, *tmp; | ||
| 227 | unsigned long flags; | ||
| 228 | int tx_drop_cnt = 0, rx_drop_cnt = 0; | ||
| 229 | |||
| 230 | /* drop all TX and Rx messages before we stall + reset DSP */ | ||
| 231 | spin_lock_irqsave(&ipc->dsp->spinlock, flags); | ||
| 232 | |||
| 233 | list_for_each_entry_safe(msg, tmp, &ipc->tx_list, list) { | ||
| 234 | list_move(&msg->list, &ipc->empty_list); | ||
| 235 | tx_drop_cnt++; | ||
| 236 | } | ||
| 237 | |||
| 238 | list_for_each_entry_safe(msg, tmp, &ipc->rx_list, list) { | ||
| 239 | list_move(&msg->list, &ipc->empty_list); | ||
| 240 | rx_drop_cnt++; | ||
| 241 | } | ||
| 242 | |||
| 243 | spin_unlock_irqrestore(&ipc->dsp->spinlock, flags); | ||
| 244 | |||
| 245 | if (tx_drop_cnt || rx_drop_cnt) | ||
| 246 | dev_err(ipc->dev, "dropped IPC msg RX=%d, TX=%d\n", | ||
| 247 | tx_drop_cnt, rx_drop_cnt); | ||
| 248 | } | ||
| 249 | EXPORT_SYMBOL_GPL(sst_ipc_drop_all); | ||
| 250 | |||
| 251 | int sst_ipc_init(struct sst_generic_ipc *ipc) | ||
| 252 | { | ||
| 253 | int ret; | ||
| 254 | |||
| 255 | INIT_LIST_HEAD(&ipc->tx_list); | ||
| 256 | INIT_LIST_HEAD(&ipc->rx_list); | ||
| 257 | INIT_LIST_HEAD(&ipc->empty_list); | ||
| 258 | init_waitqueue_head(&ipc->wait_txq); | ||
| 259 | |||
| 260 | ret = msg_empty_list_init(ipc); | ||
| 261 | if (ret < 0) | ||
| 262 | return -ENOMEM; | ||
| 263 | |||
| 264 | /* start the IPC message thread */ | ||
| 265 | init_kthread_worker(&ipc->kworker); | ||
| 266 | ipc->tx_thread = kthread_run(kthread_worker_fn, | ||
| 267 | &ipc->kworker, "%s", | ||
| 268 | dev_name(ipc->dev)); | ||
| 269 | if (IS_ERR(ipc->tx_thread)) { | ||
| 270 | dev_err(ipc->dev, "error: failed to create message TX task\n"); | ||
| 271 | ret = PTR_ERR(ipc->tx_thread); | ||
| 272 | kfree(ipc->msg); | ||
| 273 | return ret; | ||
| 274 | } | ||
| 275 | |||
| 276 | init_kthread_work(&ipc->kwork, ipc_tx_msgs); | ||
| 277 | return 0; | ||
| 278 | } | ||
| 279 | EXPORT_SYMBOL_GPL(sst_ipc_init); | ||
| 280 | |||
| 281 | void sst_ipc_fini(struct sst_generic_ipc *ipc) | ||
| 282 | { | ||
| 283 | if (ipc->tx_thread) | ||
| 284 | kthread_stop(ipc->tx_thread); | ||
| 285 | |||
| 286 | if (ipc->msg) | ||
| 287 | kfree(ipc->msg); | ||
| 288 | } | ||
| 289 | EXPORT_SYMBOL_GPL(sst_ipc_fini); | ||
| 290 | |||
| 291 | /* Module information */ | ||
| 292 | MODULE_AUTHOR("Jin Yao"); | ||
| 293 | MODULE_DESCRIPTION("Intel SST IPC generic"); | ||
| 294 | MODULE_LICENSE("GPL v2"); | ||
diff --git a/sound/soc/intel/common/sst-ipc.h b/sound/soc/intel/common/sst-ipc.h new file mode 100644 index 000000000000..125ea451a373 --- /dev/null +++ b/sound/soc/intel/common/sst-ipc.h | |||
| @@ -0,0 +1,91 @@ | |||
| 1 | /* | ||
| 2 | * Intel SST generic IPC Support | ||
| 3 | * | ||
| 4 | * Copyright (C) 2015, Intel Corporation. All rights reserved. | ||
| 5 | * | ||
| 6 | * This program is free software; you can redistribute it and/or | ||
| 7 | * modify it under the terms of the GNU General Public License version | ||
| 8 | * 2 as published by the Free Software Foundation. | ||
| 9 | * | ||
| 10 | * This program is distributed in the hope that it will be useful, | ||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | * GNU General Public License for more details. | ||
| 14 | * | ||
| 15 | */ | ||
| 16 | |||
| 17 | #ifndef __SST_GENERIC_IPC_H | ||
| 18 | #define __SST_GENERIC_IPC_H | ||
| 19 | |||
| 20 | #include <linux/types.h> | ||
| 21 | #include <linux/kernel.h> | ||
| 22 | #include <linux/wait.h> | ||
| 23 | #include <linux/list.h> | ||
| 24 | #include <linux/workqueue.h> | ||
| 25 | #include <linux/sched.h> | ||
| 26 | #include <linux/kthread.h> | ||
| 27 | |||
| 28 | #define IPC_MAX_MAILBOX_BYTES 256 | ||
| 29 | |||
| 30 | struct ipc_message { | ||
| 31 | struct list_head list; | ||
| 32 | u64 header; | ||
| 33 | |||
| 34 | /* direction wrt host CPU */ | ||
| 35 | char tx_data[IPC_MAX_MAILBOX_BYTES]; | ||
| 36 | size_t tx_size; | ||
| 37 | char rx_data[IPC_MAX_MAILBOX_BYTES]; | ||
| 38 | size_t rx_size; | ||
| 39 | |||
| 40 | wait_queue_head_t waitq; | ||
| 41 | bool pending; | ||
| 42 | bool complete; | ||
| 43 | bool wait; | ||
| 44 | int errno; | ||
| 45 | }; | ||
| 46 | |||
| 47 | struct sst_generic_ipc; | ||
| 48 | |||
| 49 | struct sst_plat_ipc_ops { | ||
| 50 | void (*tx_msg)(struct sst_generic_ipc *, struct ipc_message *); | ||
| 51 | void (*shim_dbg)(struct sst_generic_ipc *, const char *); | ||
| 52 | void (*tx_data_copy)(struct ipc_message *, char *, size_t); | ||
| 53 | u64 (*reply_msg_match)(u64 header, u64 *mask); | ||
| 54 | }; | ||
| 55 | |||
| 56 | /* SST generic IPC data */ | ||
| 57 | struct sst_generic_ipc { | ||
| 58 | struct device *dev; | ||
| 59 | struct sst_dsp *dsp; | ||
| 60 | |||
| 61 | /* IPC messaging */ | ||
| 62 | struct list_head tx_list; | ||
| 63 | struct list_head rx_list; | ||
| 64 | struct list_head empty_list; | ||
| 65 | wait_queue_head_t wait_txq; | ||
| 66 | struct task_struct *tx_thread; | ||
| 67 | struct kthread_worker kworker; | ||
| 68 | struct kthread_work kwork; | ||
| 69 | bool pending; | ||
| 70 | struct ipc_message *msg; | ||
| 71 | |||
| 72 | struct sst_plat_ipc_ops ops; | ||
| 73 | }; | ||
| 74 | |||
| 75 | int sst_ipc_tx_message_wait(struct sst_generic_ipc *ipc, u64 header, | ||
| 76 | void *tx_data, size_t tx_bytes, void *rx_data, size_t rx_bytes); | ||
| 77 | |||
| 78 | int sst_ipc_tx_message_nowait(struct sst_generic_ipc *ipc, u64 header, | ||
| 79 | void *tx_data, size_t tx_bytes); | ||
| 80 | |||
| 81 | struct ipc_message *sst_ipc_reply_find_msg(struct sst_generic_ipc *ipc, | ||
| 82 | u64 header); | ||
| 83 | |||
| 84 | void sst_ipc_tx_msg_reply_complete(struct sst_generic_ipc *ipc, | ||
| 85 | struct ipc_message *msg); | ||
| 86 | |||
| 87 | void sst_ipc_drop_all(struct sst_generic_ipc *ipc); | ||
| 88 | int sst_ipc_init(struct sst_generic_ipc *ipc); | ||
| 89 | void sst_ipc_fini(struct sst_generic_ipc *ipc); | ||
| 90 | |||
| 91 | #endif | ||
