Mark Brown | a4b1299 | 2014-03-12 23:04:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Intel Baytrail SST IPC Support |
| 3 | * Copyright (c) 2014, Intel Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/types.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/list.h> |
| 18 | #include <linux/device.h> |
| 19 | #include <linux/wait.h> |
| 20 | #include <linux/spinlock.h> |
| 21 | #include <linux/workqueue.h> |
| 22 | #include <linux/export.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/delay.h> |
Mark Brown | a4b1299 | 2014-03-12 23:04:35 +0000 | [diff] [blame] | 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/kthread.h> |
| 27 | #include <linux/firmware.h> |
| 28 | #include <linux/io.h> |
| 29 | #include <asm/div64.h> |
| 30 | |
| 31 | #include "sst-baytrail-ipc.h" |
| 32 | #include "sst-dsp.h" |
| 33 | #include "sst-dsp-priv.h" |
| 34 | |
| 35 | /* IPC message timeout */ |
| 36 | #define IPC_TIMEOUT_MSECS 300 |
| 37 | #define IPC_BOOT_MSECS 200 |
| 38 | |
| 39 | #define IPC_EMPTY_LIST_SIZE 8 |
| 40 | |
| 41 | /* IPC header bits */ |
| 42 | #define IPC_HEADER_MSG_ID_MASK 0xff |
| 43 | #define IPC_HEADER_MSG_ID(x) ((x) & IPC_HEADER_MSG_ID_MASK) |
| 44 | #define IPC_HEADER_STR_ID_SHIFT 8 |
| 45 | #define IPC_HEADER_STR_ID_MASK 0x1f |
| 46 | #define IPC_HEADER_STR_ID(x) (((x) & 0x1f) << IPC_HEADER_STR_ID_SHIFT) |
| 47 | #define IPC_HEADER_LARGE_SHIFT 13 |
| 48 | #define IPC_HEADER_LARGE(x) (((x) & 0x1) << IPC_HEADER_LARGE_SHIFT) |
| 49 | #define IPC_HEADER_DATA_SHIFT 16 |
| 50 | #define IPC_HEADER_DATA_MASK 0x3fff |
| 51 | #define IPC_HEADER_DATA(x) (((x) & 0x3fff) << IPC_HEADER_DATA_SHIFT) |
| 52 | |
| 53 | /* mask for differentiating between notification and reply message */ |
| 54 | #define IPC_NOTIFICATION (0x1 << 7) |
| 55 | |
| 56 | /* I2L Stream config/control msgs */ |
| 57 | #define IPC_IA_ALLOC_STREAM 0x20 |
| 58 | #define IPC_IA_FREE_STREAM 0x21 |
| 59 | #define IPC_IA_PAUSE_STREAM 0x24 |
| 60 | #define IPC_IA_RESUME_STREAM 0x25 |
| 61 | #define IPC_IA_DROP_STREAM 0x26 |
| 62 | #define IPC_IA_START_STREAM 0x30 |
| 63 | |
| 64 | /* notification messages */ |
| 65 | #define IPC_IA_FW_INIT_CMPLT 0x81 |
| 66 | #define IPC_SST_PERIOD_ELAPSED 0x97 |
| 67 | |
| 68 | /* IPC messages between host and ADSP */ |
| 69 | struct sst_byt_address_info { |
| 70 | u32 addr; |
| 71 | u32 size; |
| 72 | } __packed; |
| 73 | |
| 74 | struct sst_byt_str_type { |
| 75 | u8 codec_type; |
| 76 | u8 str_type; |
| 77 | u8 operation; |
| 78 | u8 protected_str; |
| 79 | u8 time_slots; |
| 80 | u8 reserved; |
| 81 | u16 result; |
| 82 | } __packed; |
| 83 | |
| 84 | struct sst_byt_pcm_params { |
| 85 | u8 num_chan; |
| 86 | u8 pcm_wd_sz; |
| 87 | u8 use_offload_path; |
| 88 | u8 reserved; |
| 89 | u32 sfreq; |
| 90 | u8 channel_map[8]; |
| 91 | } __packed; |
| 92 | |
| 93 | struct sst_byt_frames_info { |
| 94 | u16 num_entries; |
| 95 | u16 rsrvd; |
| 96 | u32 frag_size; |
| 97 | struct sst_byt_address_info ring_buf_info[8]; |
| 98 | } __packed; |
| 99 | |
| 100 | struct sst_byt_alloc_params { |
| 101 | struct sst_byt_str_type str_type; |
| 102 | struct sst_byt_pcm_params pcm_params; |
| 103 | struct sst_byt_frames_info frame_info; |
| 104 | } __packed; |
| 105 | |
| 106 | struct sst_byt_alloc_response { |
| 107 | struct sst_byt_str_type str_type; |
| 108 | u8 reserved[88]; |
| 109 | } __packed; |
| 110 | |
| 111 | struct sst_byt_start_stream_params { |
| 112 | u32 byte_offset; |
| 113 | } __packed; |
| 114 | |
| 115 | struct sst_byt_tstamp { |
| 116 | u64 ring_buffer_counter; |
| 117 | u64 hardware_counter; |
| 118 | u64 frames_decoded; |
| 119 | u64 bytes_decoded; |
| 120 | u64 bytes_copied; |
| 121 | u32 sampling_frequency; |
| 122 | u32 channel_peak[8]; |
| 123 | } __packed; |
| 124 | |
| 125 | /* driver internal IPC message structure */ |
| 126 | struct ipc_message { |
| 127 | struct list_head list; |
| 128 | u64 header; |
| 129 | |
| 130 | /* direction wrt host CPU */ |
| 131 | char tx_data[SST_BYT_IPC_MAX_PAYLOAD_SIZE]; |
| 132 | size_t tx_size; |
| 133 | char rx_data[SST_BYT_IPC_MAX_PAYLOAD_SIZE]; |
| 134 | size_t rx_size; |
| 135 | |
| 136 | wait_queue_head_t waitq; |
| 137 | bool complete; |
| 138 | bool wait; |
| 139 | int errno; |
| 140 | }; |
| 141 | |
| 142 | struct sst_byt_stream; |
| 143 | struct sst_byt; |
| 144 | |
| 145 | /* stream infomation */ |
| 146 | struct sst_byt_stream { |
| 147 | struct list_head node; |
| 148 | |
| 149 | /* configuration */ |
| 150 | struct sst_byt_alloc_params request; |
| 151 | struct sst_byt_alloc_response reply; |
| 152 | |
| 153 | /* runtime info */ |
| 154 | struct sst_byt *byt; |
| 155 | int str_id; |
| 156 | bool commited; |
| 157 | bool running; |
| 158 | |
| 159 | /* driver callback */ |
| 160 | u32 (*notify_position)(struct sst_byt_stream *stream, void *data); |
| 161 | void *pdata; |
| 162 | }; |
| 163 | |
| 164 | /* SST Baytrail IPC data */ |
| 165 | struct sst_byt { |
| 166 | struct device *dev; |
| 167 | struct sst_dsp *dsp; |
| 168 | |
| 169 | /* stream */ |
| 170 | struct list_head stream_list; |
| 171 | |
| 172 | /* boot */ |
| 173 | wait_queue_head_t boot_wait; |
| 174 | bool boot_complete; |
Liam Girdwood | af94aa5 | 2014-05-08 16:07:26 +0300 | [diff] [blame] | 175 | struct sst_fw *fw; |
Mark Brown | a4b1299 | 2014-03-12 23:04:35 +0000 | [diff] [blame] | 176 | |
| 177 | /* IPC messaging */ |
| 178 | struct list_head tx_list; |
| 179 | struct list_head rx_list; |
| 180 | struct list_head empty_list; |
| 181 | wait_queue_head_t wait_txq; |
| 182 | struct task_struct *tx_thread; |
| 183 | struct kthread_worker kworker; |
| 184 | struct kthread_work kwork; |
| 185 | struct ipc_message *msg; |
| 186 | }; |
| 187 | |
| 188 | static inline u64 sst_byt_header(int msg_id, int data, bool large, int str_id) |
| 189 | { |
| 190 | u64 header; |
| 191 | |
| 192 | header = IPC_HEADER_MSG_ID(msg_id) | |
| 193 | IPC_HEADER_STR_ID(str_id) | |
| 194 | IPC_HEADER_LARGE(large) | |
| 195 | IPC_HEADER_DATA(data) | |
| 196 | SST_BYT_IPCX_BUSY; |
| 197 | |
| 198 | return header; |
| 199 | } |
| 200 | |
| 201 | static inline u16 sst_byt_header_msg_id(u64 header) |
| 202 | { |
| 203 | return header & IPC_HEADER_MSG_ID_MASK; |
| 204 | } |
| 205 | |
| 206 | static inline u8 sst_byt_header_str_id(u64 header) |
| 207 | { |
| 208 | return (header >> IPC_HEADER_STR_ID_SHIFT) & IPC_HEADER_STR_ID_MASK; |
| 209 | } |
| 210 | |
| 211 | static inline u16 sst_byt_header_data(u64 header) |
| 212 | { |
| 213 | return (header >> IPC_HEADER_DATA_SHIFT) & IPC_HEADER_DATA_MASK; |
| 214 | } |
| 215 | |
| 216 | static struct sst_byt_stream *sst_byt_get_stream(struct sst_byt *byt, |
| 217 | int stream_id) |
| 218 | { |
| 219 | struct sst_byt_stream *stream; |
| 220 | |
| 221 | list_for_each_entry(stream, &byt->stream_list, node) { |
| 222 | if (stream->str_id == stream_id) |
| 223 | return stream; |
| 224 | } |
| 225 | |
| 226 | return NULL; |
| 227 | } |
| 228 | |
| 229 | static void sst_byt_ipc_shim_dbg(struct sst_byt *byt, const char *text) |
| 230 | { |
| 231 | struct sst_dsp *sst = byt->dsp; |
| 232 | u64 isr, ipcd, imrx, ipcx; |
| 233 | |
| 234 | ipcx = sst_dsp_shim_read64_unlocked(sst, SST_IPCX); |
| 235 | isr = sst_dsp_shim_read64_unlocked(sst, SST_ISRX); |
| 236 | ipcd = sst_dsp_shim_read64_unlocked(sst, SST_IPCD); |
| 237 | imrx = sst_dsp_shim_read64_unlocked(sst, SST_IMRX); |
| 238 | |
| 239 | dev_err(byt->dev, |
| 240 | "ipc: --%s-- ipcx 0x%llx isr 0x%llx ipcd 0x%llx imrx 0x%llx\n", |
| 241 | text, ipcx, isr, ipcd, imrx); |
| 242 | } |
| 243 | |
| 244 | /* locks held by caller */ |
| 245 | static struct ipc_message *sst_byt_msg_get_empty(struct sst_byt *byt) |
| 246 | { |
| 247 | struct ipc_message *msg = NULL; |
| 248 | |
| 249 | if (!list_empty(&byt->empty_list)) { |
| 250 | msg = list_first_entry(&byt->empty_list, |
| 251 | struct ipc_message, list); |
| 252 | list_del(&msg->list); |
| 253 | } |
| 254 | |
| 255 | return msg; |
| 256 | } |
| 257 | |
| 258 | static void sst_byt_ipc_tx_msgs(struct kthread_work *work) |
| 259 | { |
| 260 | struct sst_byt *byt = |
| 261 | container_of(work, struct sst_byt, kwork); |
| 262 | struct ipc_message *msg; |
| 263 | u64 ipcx; |
| 264 | unsigned long flags; |
| 265 | |
| 266 | spin_lock_irqsave(&byt->dsp->spinlock, flags); |
| 267 | if (list_empty(&byt->tx_list)) { |
| 268 | spin_unlock_irqrestore(&byt->dsp->spinlock, flags); |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | /* if the DSP is busy we will TX messages after IRQ */ |
| 273 | ipcx = sst_dsp_shim_read64_unlocked(byt->dsp, SST_IPCX); |
| 274 | if (ipcx & SST_BYT_IPCX_BUSY) { |
| 275 | spin_unlock_irqrestore(&byt->dsp->spinlock, flags); |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | msg = list_first_entry(&byt->tx_list, struct ipc_message, list); |
| 280 | |
| 281 | list_move(&msg->list, &byt->rx_list); |
| 282 | |
| 283 | /* send the message */ |
| 284 | if (msg->header & IPC_HEADER_LARGE(true)) |
| 285 | sst_dsp_outbox_write(byt->dsp, msg->tx_data, msg->tx_size); |
| 286 | sst_dsp_shim_write64_unlocked(byt->dsp, SST_IPCX, msg->header); |
| 287 | |
| 288 | spin_unlock_irqrestore(&byt->dsp->spinlock, flags); |
| 289 | } |
| 290 | |
| 291 | static inline void sst_byt_tx_msg_reply_complete(struct sst_byt *byt, |
| 292 | struct ipc_message *msg) |
| 293 | { |
| 294 | msg->complete = true; |
| 295 | |
| 296 | if (!msg->wait) |
| 297 | list_add_tail(&msg->list, &byt->empty_list); |
| 298 | else |
| 299 | wake_up(&msg->waitq); |
| 300 | } |
| 301 | |
Liam Girdwood | 609a13e | 2014-05-08 16:07:25 +0300 | [diff] [blame] | 302 | static void sst_byt_drop_all(struct sst_byt *byt) |
| 303 | { |
| 304 | struct ipc_message *msg, *tmp; |
| 305 | unsigned long flags; |
| 306 | |
| 307 | /* drop all TX and Rx messages before we stall + reset DSP */ |
| 308 | spin_lock_irqsave(&byt->dsp->spinlock, flags); |
| 309 | list_for_each_entry_safe(msg, tmp, &byt->tx_list, list) { |
| 310 | list_move(&msg->list, &byt->empty_list); |
| 311 | } |
| 312 | |
| 313 | list_for_each_entry_safe(msg, tmp, &byt->rx_list, list) { |
| 314 | list_move(&msg->list, &byt->empty_list); |
| 315 | } |
| 316 | |
| 317 | spin_unlock_irqrestore(&byt->dsp->spinlock, flags); |
| 318 | } |
| 319 | |
Mark Brown | a4b1299 | 2014-03-12 23:04:35 +0000 | [diff] [blame] | 320 | static int sst_byt_tx_wait_done(struct sst_byt *byt, struct ipc_message *msg, |
| 321 | void *rx_data) |
| 322 | { |
| 323 | unsigned long flags; |
| 324 | int ret; |
| 325 | |
| 326 | /* wait for DSP completion */ |
| 327 | ret = wait_event_timeout(msg->waitq, msg->complete, |
| 328 | msecs_to_jiffies(IPC_TIMEOUT_MSECS)); |
| 329 | |
| 330 | spin_lock_irqsave(&byt->dsp->spinlock, flags); |
| 331 | if (ret == 0) { |
| 332 | list_del(&msg->list); |
| 333 | sst_byt_ipc_shim_dbg(byt, "message timeout"); |
| 334 | |
| 335 | ret = -ETIMEDOUT; |
| 336 | } else { |
| 337 | |
| 338 | /* copy the data returned from DSP */ |
| 339 | if (msg->rx_size) |
| 340 | memcpy(rx_data, msg->rx_data, msg->rx_size); |
| 341 | ret = msg->errno; |
| 342 | } |
| 343 | |
| 344 | list_add_tail(&msg->list, &byt->empty_list); |
| 345 | spin_unlock_irqrestore(&byt->dsp->spinlock, flags); |
| 346 | return ret; |
| 347 | } |
| 348 | |
| 349 | static int sst_byt_ipc_tx_message(struct sst_byt *byt, u64 header, |
| 350 | void *tx_data, size_t tx_bytes, |
| 351 | void *rx_data, size_t rx_bytes, int wait) |
| 352 | { |
| 353 | unsigned long flags; |
| 354 | struct ipc_message *msg; |
| 355 | |
| 356 | spin_lock_irqsave(&byt->dsp->spinlock, flags); |
| 357 | |
| 358 | msg = sst_byt_msg_get_empty(byt); |
| 359 | if (msg == NULL) { |
| 360 | spin_unlock_irqrestore(&byt->dsp->spinlock, flags); |
| 361 | return -EBUSY; |
| 362 | } |
| 363 | |
| 364 | msg->header = header; |
| 365 | msg->tx_size = tx_bytes; |
| 366 | msg->rx_size = rx_bytes; |
| 367 | msg->wait = wait; |
| 368 | msg->errno = 0; |
| 369 | msg->complete = false; |
| 370 | |
| 371 | if (tx_bytes) { |
| 372 | /* msg content = lower 32-bit of the header + data */ |
| 373 | *(u32 *)msg->tx_data = (u32)(header & (u32)-1); |
| 374 | memcpy(msg->tx_data + sizeof(u32), tx_data, tx_bytes); |
| 375 | msg->tx_size += sizeof(u32); |
| 376 | } |
| 377 | |
| 378 | list_add_tail(&msg->list, &byt->tx_list); |
| 379 | spin_unlock_irqrestore(&byt->dsp->spinlock, flags); |
| 380 | |
| 381 | queue_kthread_work(&byt->kworker, &byt->kwork); |
| 382 | |
| 383 | if (wait) |
| 384 | return sst_byt_tx_wait_done(byt, msg, rx_data); |
| 385 | else |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | static inline int sst_byt_ipc_tx_msg_wait(struct sst_byt *byt, u64 header, |
| 390 | void *tx_data, size_t tx_bytes, |
| 391 | void *rx_data, size_t rx_bytes) |
| 392 | { |
| 393 | return sst_byt_ipc_tx_message(byt, header, tx_data, tx_bytes, |
| 394 | rx_data, rx_bytes, 1); |
| 395 | } |
| 396 | |
| 397 | static inline int sst_byt_ipc_tx_msg_nowait(struct sst_byt *byt, u64 header, |
| 398 | void *tx_data, size_t tx_bytes) |
| 399 | { |
| 400 | return sst_byt_ipc_tx_message(byt, header, tx_data, tx_bytes, |
| 401 | NULL, 0, 0); |
| 402 | } |
| 403 | |
| 404 | static struct ipc_message *sst_byt_reply_find_msg(struct sst_byt *byt, |
| 405 | u64 header) |
| 406 | { |
| 407 | struct ipc_message *msg = NULL, *_msg; |
| 408 | u64 mask; |
| 409 | |
| 410 | /* match reply to message sent based on msg and stream IDs */ |
| 411 | mask = IPC_HEADER_MSG_ID_MASK | |
| 412 | IPC_HEADER_STR_ID_MASK << IPC_HEADER_STR_ID_SHIFT; |
| 413 | header &= mask; |
| 414 | |
| 415 | if (list_empty(&byt->rx_list)) { |
| 416 | dev_err(byt->dev, |
| 417 | "ipc: rx list is empty but received 0x%llx\n", header); |
| 418 | goto out; |
| 419 | } |
| 420 | |
| 421 | list_for_each_entry(_msg, &byt->rx_list, list) { |
| 422 | if ((_msg->header & mask) == header) { |
| 423 | msg = _msg; |
| 424 | break; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | out: |
| 429 | return msg; |
| 430 | } |
| 431 | |
| 432 | static void sst_byt_stream_update(struct sst_byt *byt, struct ipc_message *msg) |
| 433 | { |
| 434 | struct sst_byt_stream *stream; |
| 435 | u64 header = msg->header; |
| 436 | u8 stream_id = sst_byt_header_str_id(header); |
| 437 | u8 stream_msg = sst_byt_header_msg_id(header); |
| 438 | |
| 439 | stream = sst_byt_get_stream(byt, stream_id); |
| 440 | if (stream == NULL) |
| 441 | return; |
| 442 | |
| 443 | switch (stream_msg) { |
| 444 | case IPC_IA_DROP_STREAM: |
| 445 | case IPC_IA_PAUSE_STREAM: |
| 446 | case IPC_IA_FREE_STREAM: |
| 447 | stream->running = false; |
| 448 | break; |
| 449 | case IPC_IA_START_STREAM: |
| 450 | case IPC_IA_RESUME_STREAM: |
| 451 | stream->running = true; |
| 452 | break; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | static int sst_byt_process_reply(struct sst_byt *byt, u64 header) |
| 457 | { |
| 458 | struct ipc_message *msg; |
| 459 | |
| 460 | msg = sst_byt_reply_find_msg(byt, header); |
| 461 | if (msg == NULL) |
| 462 | return 1; |
| 463 | |
| 464 | if (header & IPC_HEADER_LARGE(true)) { |
| 465 | msg->rx_size = sst_byt_header_data(header); |
| 466 | sst_dsp_inbox_read(byt->dsp, msg->rx_data, msg->rx_size); |
| 467 | } |
| 468 | |
| 469 | /* update any stream states */ |
| 470 | sst_byt_stream_update(byt, msg); |
| 471 | |
| 472 | list_del(&msg->list); |
| 473 | /* wake up */ |
| 474 | sst_byt_tx_msg_reply_complete(byt, msg); |
| 475 | |
| 476 | return 1; |
| 477 | } |
| 478 | |
| 479 | static void sst_byt_fw_ready(struct sst_byt *byt, u64 header) |
| 480 | { |
| 481 | dev_dbg(byt->dev, "ipc: DSP is ready 0x%llX\n", header); |
| 482 | |
| 483 | byt->boot_complete = true; |
| 484 | wake_up(&byt->boot_wait); |
| 485 | } |
| 486 | |
| 487 | static int sst_byt_process_notification(struct sst_byt *byt, |
| 488 | unsigned long *flags) |
| 489 | { |
| 490 | struct sst_dsp *sst = byt->dsp; |
| 491 | struct sst_byt_stream *stream; |
| 492 | u64 header; |
| 493 | u8 msg_id, stream_id; |
| 494 | int handled = 1; |
| 495 | |
| 496 | header = sst_dsp_shim_read64_unlocked(sst, SST_IPCD); |
| 497 | msg_id = sst_byt_header_msg_id(header); |
| 498 | |
| 499 | switch (msg_id) { |
| 500 | case IPC_SST_PERIOD_ELAPSED: |
| 501 | stream_id = sst_byt_header_str_id(header); |
| 502 | stream = sst_byt_get_stream(byt, stream_id); |
| 503 | if (stream && stream->running && stream->notify_position) { |
| 504 | spin_unlock_irqrestore(&sst->spinlock, *flags); |
| 505 | stream->notify_position(stream, stream->pdata); |
| 506 | spin_lock_irqsave(&sst->spinlock, *flags); |
| 507 | } |
| 508 | break; |
| 509 | case IPC_IA_FW_INIT_CMPLT: |
| 510 | sst_byt_fw_ready(byt, header); |
| 511 | break; |
| 512 | } |
| 513 | |
| 514 | return handled; |
| 515 | } |
| 516 | |
| 517 | static irqreturn_t sst_byt_irq_thread(int irq, void *context) |
| 518 | { |
| 519 | struct sst_dsp *sst = (struct sst_dsp *) context; |
| 520 | struct sst_byt *byt = sst_dsp_get_thread_context(sst); |
| 521 | u64 header; |
| 522 | unsigned long flags; |
| 523 | |
| 524 | spin_lock_irqsave(&sst->spinlock, flags); |
| 525 | |
| 526 | header = sst_dsp_shim_read64_unlocked(sst, SST_IPCD); |
| 527 | if (header & SST_BYT_IPCD_BUSY) { |
| 528 | if (header & IPC_NOTIFICATION) { |
| 529 | /* message from ADSP */ |
| 530 | sst_byt_process_notification(byt, &flags); |
| 531 | } else { |
| 532 | /* reply from ADSP */ |
| 533 | sst_byt_process_reply(byt, header); |
| 534 | } |
| 535 | /* |
| 536 | * clear IPCD BUSY bit and set DONE bit. Tell DSP we have |
| 537 | * processed the message and can accept new. Clear data part |
| 538 | * of the header |
| 539 | */ |
| 540 | sst_dsp_shim_update_bits64_unlocked(sst, SST_IPCD, |
| 541 | SST_BYT_IPCD_DONE | SST_BYT_IPCD_BUSY | |
| 542 | IPC_HEADER_DATA(IPC_HEADER_DATA_MASK), |
| 543 | SST_BYT_IPCD_DONE); |
| 544 | /* unmask message request interrupts */ |
| 545 | sst_dsp_shim_update_bits64_unlocked(sst, SST_IMRX, |
| 546 | SST_BYT_IMRX_REQUEST, 0); |
| 547 | } |
| 548 | |
| 549 | spin_unlock_irqrestore(&sst->spinlock, flags); |
| 550 | |
| 551 | /* continue to send any remaining messages... */ |
| 552 | queue_kthread_work(&byt->kworker, &byt->kwork); |
| 553 | |
| 554 | return IRQ_HANDLED; |
| 555 | } |
| 556 | |
| 557 | /* stream API */ |
| 558 | struct sst_byt_stream *sst_byt_stream_new(struct sst_byt *byt, int id, |
| 559 | u32 (*notify_position)(struct sst_byt_stream *stream, void *data), |
| 560 | void *data) |
| 561 | { |
| 562 | struct sst_byt_stream *stream; |
Wenkai Du | d132cb0 | 2014-04-23 13:29:30 +0300 | [diff] [blame] | 563 | struct sst_dsp *sst = byt->dsp; |
| 564 | unsigned long flags; |
Mark Brown | a4b1299 | 2014-03-12 23:04:35 +0000 | [diff] [blame] | 565 | |
| 566 | stream = kzalloc(sizeof(*stream), GFP_KERNEL); |
| 567 | if (stream == NULL) |
| 568 | return NULL; |
| 569 | |
Wenkai Du | d132cb0 | 2014-04-23 13:29:30 +0300 | [diff] [blame] | 570 | spin_lock_irqsave(&sst->spinlock, flags); |
Mark Brown | a4b1299 | 2014-03-12 23:04:35 +0000 | [diff] [blame] | 571 | list_add(&stream->node, &byt->stream_list); |
| 572 | stream->notify_position = notify_position; |
| 573 | stream->pdata = data; |
| 574 | stream->byt = byt; |
| 575 | stream->str_id = id; |
Wenkai Du | d132cb0 | 2014-04-23 13:29:30 +0300 | [diff] [blame] | 576 | spin_unlock_irqrestore(&sst->spinlock, flags); |
Mark Brown | a4b1299 | 2014-03-12 23:04:35 +0000 | [diff] [blame] | 577 | |
| 578 | return stream; |
| 579 | } |
| 580 | |
| 581 | int sst_byt_stream_set_bits(struct sst_byt *byt, struct sst_byt_stream *stream, |
| 582 | int bits) |
| 583 | { |
| 584 | stream->request.pcm_params.pcm_wd_sz = bits; |
| 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | int sst_byt_stream_set_channels(struct sst_byt *byt, |
| 589 | struct sst_byt_stream *stream, u8 channels) |
| 590 | { |
| 591 | stream->request.pcm_params.num_chan = channels; |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | int sst_byt_stream_set_rate(struct sst_byt *byt, struct sst_byt_stream *stream, |
| 596 | unsigned int rate) |
| 597 | { |
| 598 | stream->request.pcm_params.sfreq = rate; |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | /* stream sonfiguration */ |
| 603 | int sst_byt_stream_type(struct sst_byt *byt, struct sst_byt_stream *stream, |
| 604 | int codec_type, int stream_type, int operation) |
| 605 | { |
| 606 | stream->request.str_type.codec_type = codec_type; |
| 607 | stream->request.str_type.str_type = stream_type; |
| 608 | stream->request.str_type.operation = operation; |
| 609 | stream->request.str_type.time_slots = 0xc; |
| 610 | |
| 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | int sst_byt_stream_buffer(struct sst_byt *byt, struct sst_byt_stream *stream, |
| 615 | uint32_t buffer_addr, uint32_t buffer_size) |
| 616 | { |
| 617 | stream->request.frame_info.num_entries = 1; |
| 618 | stream->request.frame_info.ring_buf_info[0].addr = buffer_addr; |
| 619 | stream->request.frame_info.ring_buf_info[0].size = buffer_size; |
| 620 | /* calculate bytes per 4 ms fragment */ |
| 621 | stream->request.frame_info.frag_size = |
| 622 | stream->request.pcm_params.sfreq * |
| 623 | stream->request.pcm_params.num_chan * |
| 624 | stream->request.pcm_params.pcm_wd_sz / 8 * |
| 625 | 4 / 1000; |
| 626 | return 0; |
| 627 | } |
| 628 | |
| 629 | int sst_byt_stream_commit(struct sst_byt *byt, struct sst_byt_stream *stream) |
| 630 | { |
| 631 | struct sst_byt_alloc_params *str_req = &stream->request; |
| 632 | struct sst_byt_alloc_response *reply = &stream->reply; |
| 633 | u64 header; |
| 634 | int ret; |
| 635 | |
| 636 | header = sst_byt_header(IPC_IA_ALLOC_STREAM, |
| 637 | sizeof(*str_req) + sizeof(u32), |
| 638 | true, stream->str_id); |
| 639 | ret = sst_byt_ipc_tx_msg_wait(byt, header, str_req, sizeof(*str_req), |
| 640 | reply, sizeof(*reply)); |
| 641 | if (ret < 0) { |
| 642 | dev_err(byt->dev, "ipc: error stream commit failed\n"); |
| 643 | return ret; |
| 644 | } |
| 645 | |
| 646 | stream->commited = true; |
| 647 | |
| 648 | return 0; |
| 649 | } |
| 650 | |
| 651 | int sst_byt_stream_free(struct sst_byt *byt, struct sst_byt_stream *stream) |
| 652 | { |
| 653 | u64 header; |
| 654 | int ret = 0; |
Wenkai Du | d132cb0 | 2014-04-23 13:29:30 +0300 | [diff] [blame] | 655 | struct sst_dsp *sst = byt->dsp; |
| 656 | unsigned long flags; |
Mark Brown | a4b1299 | 2014-03-12 23:04:35 +0000 | [diff] [blame] | 657 | |
| 658 | if (!stream->commited) |
| 659 | goto out; |
| 660 | |
| 661 | header = sst_byt_header(IPC_IA_FREE_STREAM, 0, false, stream->str_id); |
| 662 | ret = sst_byt_ipc_tx_msg_wait(byt, header, NULL, 0, NULL, 0); |
| 663 | if (ret < 0) { |
| 664 | dev_err(byt->dev, "ipc: free stream %d failed\n", |
| 665 | stream->str_id); |
| 666 | return -EAGAIN; |
| 667 | } |
| 668 | |
| 669 | stream->commited = false; |
| 670 | out: |
Wenkai Du | d132cb0 | 2014-04-23 13:29:30 +0300 | [diff] [blame] | 671 | spin_lock_irqsave(&sst->spinlock, flags); |
Mark Brown | a4b1299 | 2014-03-12 23:04:35 +0000 | [diff] [blame] | 672 | list_del(&stream->node); |
| 673 | kfree(stream); |
Wenkai Du | d132cb0 | 2014-04-23 13:29:30 +0300 | [diff] [blame] | 674 | spin_unlock_irqrestore(&sst->spinlock, flags); |
Mark Brown | a4b1299 | 2014-03-12 23:04:35 +0000 | [diff] [blame] | 675 | |
| 676 | return ret; |
| 677 | } |
| 678 | |
| 679 | static int sst_byt_stream_operations(struct sst_byt *byt, int type, |
| 680 | int stream_id, int wait) |
| 681 | { |
Mark Brown | a4b1299 | 2014-03-12 23:04:35 +0000 | [diff] [blame] | 682 | u64 header; |
Mark Brown | a4b1299 | 2014-03-12 23:04:35 +0000 | [diff] [blame] | 683 | |
Jarkko Nikula | 65ee9e8 | 2014-05-08 16:07:22 +0300 | [diff] [blame] | 684 | header = sst_byt_header(type, 0, false, stream_id); |
Mark Brown | a4b1299 | 2014-03-12 23:04:35 +0000 | [diff] [blame] | 685 | if (wait) |
Jarkko Nikula | 65ee9e8 | 2014-05-08 16:07:22 +0300 | [diff] [blame] | 686 | return sst_byt_ipc_tx_msg_wait(byt, header, NULL, 0, NULL, 0); |
Mark Brown | a4b1299 | 2014-03-12 23:04:35 +0000 | [diff] [blame] | 687 | else |
Jarkko Nikula | 65ee9e8 | 2014-05-08 16:07:22 +0300 | [diff] [blame] | 688 | return sst_byt_ipc_tx_msg_nowait(byt, header, NULL, 0); |
Mark Brown | a4b1299 | 2014-03-12 23:04:35 +0000 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | /* stream ALSA trigger operations */ |
Jarkko Nikula | a6686ed | 2014-05-08 16:07:23 +0300 | [diff] [blame] | 692 | int sst_byt_stream_start(struct sst_byt *byt, struct sst_byt_stream *stream, |
| 693 | u32 start_offset) |
Mark Brown | a4b1299 | 2014-03-12 23:04:35 +0000 | [diff] [blame] | 694 | { |
Jarkko Nikula | 65ee9e8 | 2014-05-08 16:07:22 +0300 | [diff] [blame] | 695 | struct sst_byt_start_stream_params start_stream; |
| 696 | void *tx_msg; |
| 697 | size_t size; |
| 698 | u64 header; |
Mark Brown | a4b1299 | 2014-03-12 23:04:35 +0000 | [diff] [blame] | 699 | int ret; |
| 700 | |
Jarkko Nikula | a6686ed | 2014-05-08 16:07:23 +0300 | [diff] [blame] | 701 | start_stream.byte_offset = start_offset; |
Jarkko Nikula | 65ee9e8 | 2014-05-08 16:07:22 +0300 | [diff] [blame] | 702 | header = sst_byt_header(IPC_IA_START_STREAM, |
| 703 | sizeof(start_stream) + sizeof(u32), |
| 704 | true, stream->str_id); |
| 705 | tx_msg = &start_stream; |
| 706 | size = sizeof(start_stream); |
| 707 | |
| 708 | ret = sst_byt_ipc_tx_msg_nowait(byt, header, tx_msg, size); |
Mark Brown | a4b1299 | 2014-03-12 23:04:35 +0000 | [diff] [blame] | 709 | if (ret < 0) |
| 710 | dev_err(byt->dev, "ipc: error failed to start stream %d\n", |
| 711 | stream->str_id); |
| 712 | |
| 713 | return ret; |
| 714 | } |
| 715 | |
| 716 | int sst_byt_stream_stop(struct sst_byt *byt, struct sst_byt_stream *stream) |
| 717 | { |
| 718 | int ret; |
| 719 | |
| 720 | /* don't stop streams that are not commited */ |
| 721 | if (!stream->commited) |
| 722 | return 0; |
| 723 | |
| 724 | ret = sst_byt_stream_operations(byt, IPC_IA_DROP_STREAM, |
| 725 | stream->str_id, 0); |
| 726 | if (ret < 0) |
| 727 | dev_err(byt->dev, "ipc: error failed to stop stream %d\n", |
| 728 | stream->str_id); |
| 729 | return ret; |
| 730 | } |
| 731 | |
| 732 | int sst_byt_stream_pause(struct sst_byt *byt, struct sst_byt_stream *stream) |
| 733 | { |
| 734 | int ret; |
| 735 | |
| 736 | ret = sst_byt_stream_operations(byt, IPC_IA_PAUSE_STREAM, |
| 737 | stream->str_id, 0); |
| 738 | if (ret < 0) |
| 739 | dev_err(byt->dev, "ipc: error failed to pause stream %d\n", |
| 740 | stream->str_id); |
| 741 | |
| 742 | return ret; |
| 743 | } |
| 744 | |
| 745 | int sst_byt_stream_resume(struct sst_byt *byt, struct sst_byt_stream *stream) |
| 746 | { |
| 747 | int ret; |
| 748 | |
| 749 | ret = sst_byt_stream_operations(byt, IPC_IA_RESUME_STREAM, |
| 750 | stream->str_id, 0); |
| 751 | if (ret < 0) |
| 752 | dev_err(byt->dev, "ipc: error failed to resume stream %d\n", |
| 753 | stream->str_id); |
| 754 | |
| 755 | return ret; |
| 756 | } |
| 757 | |
| 758 | int sst_byt_get_dsp_position(struct sst_byt *byt, |
| 759 | struct sst_byt_stream *stream, int buffer_size) |
| 760 | { |
| 761 | struct sst_dsp *sst = byt->dsp; |
| 762 | struct sst_byt_tstamp fw_tstamp; |
| 763 | u8 str_id = stream->str_id; |
| 764 | u32 tstamp_offset; |
| 765 | |
| 766 | tstamp_offset = SST_BYT_TIMESTAMP_OFFSET + str_id * sizeof(fw_tstamp); |
| 767 | memcpy_fromio(&fw_tstamp, |
| 768 | sst->addr.lpe + tstamp_offset, sizeof(fw_tstamp)); |
| 769 | |
| 770 | return do_div(fw_tstamp.ring_buffer_counter, buffer_size); |
| 771 | } |
| 772 | |
| 773 | static int msg_empty_list_init(struct sst_byt *byt) |
| 774 | { |
| 775 | struct ipc_message *msg; |
| 776 | int i; |
| 777 | |
| 778 | byt->msg = kzalloc(sizeof(*msg) * IPC_EMPTY_LIST_SIZE, GFP_KERNEL); |
| 779 | if (byt->msg == NULL) |
| 780 | return -ENOMEM; |
| 781 | |
| 782 | for (i = 0; i < IPC_EMPTY_LIST_SIZE; i++) { |
| 783 | init_waitqueue_head(&byt->msg[i].waitq); |
| 784 | list_add(&byt->msg[i].list, &byt->empty_list); |
| 785 | } |
| 786 | |
| 787 | return 0; |
| 788 | } |
| 789 | |
| 790 | struct sst_dsp *sst_byt_get_dsp(struct sst_byt *byt) |
| 791 | { |
| 792 | return byt->dsp; |
| 793 | } |
| 794 | |
| 795 | static struct sst_dsp_device byt_dev = { |
| 796 | .thread = sst_byt_irq_thread, |
| 797 | .ops = &sst_byt_ops, |
| 798 | }; |
| 799 | |
Liam Girdwood | af94aa5 | 2014-05-08 16:07:26 +0300 | [diff] [blame] | 800 | int sst_byt_dsp_suspend_noirq(struct device *dev, struct sst_pdata *pdata) |
| 801 | { |
| 802 | struct sst_byt *byt = pdata->dsp; |
| 803 | |
| 804 | dev_dbg(byt->dev, "dsp reset\n"); |
| 805 | sst_dsp_reset(byt->dsp); |
| 806 | sst_byt_drop_all(byt); |
| 807 | dev_dbg(byt->dev, "dsp in reset\n"); |
| 808 | |
| 809 | return 0; |
| 810 | } |
| 811 | EXPORT_SYMBOL_GPL(sst_byt_dsp_suspend_noirq); |
| 812 | |
| 813 | int sst_byt_dsp_suspend_late(struct device *dev, struct sst_pdata *pdata) |
| 814 | { |
| 815 | struct sst_byt *byt = pdata->dsp; |
| 816 | |
| 817 | dev_dbg(byt->dev, "free all blocks and unload fw\n"); |
| 818 | sst_fw_unload(byt->fw); |
| 819 | |
| 820 | return 0; |
| 821 | } |
| 822 | EXPORT_SYMBOL_GPL(sst_byt_dsp_suspend_late); |
| 823 | |
| 824 | int sst_byt_dsp_boot(struct device *dev, struct sst_pdata *pdata) |
| 825 | { |
| 826 | struct sst_byt *byt = pdata->dsp; |
| 827 | int ret; |
| 828 | |
| 829 | dev_dbg(byt->dev, "reload dsp fw\n"); |
| 830 | |
| 831 | sst_dsp_reset(byt->dsp); |
| 832 | |
| 833 | ret = sst_fw_reload(byt->fw); |
| 834 | if (ret < 0) { |
| 835 | dev_err(dev, "error: failed to reload firmware\n"); |
| 836 | return ret; |
| 837 | } |
| 838 | |
| 839 | /* wait for DSP boot completion */ |
| 840 | byt->boot_complete = false; |
| 841 | sst_dsp_boot(byt->dsp); |
| 842 | dev_dbg(byt->dev, "dsp booting...\n"); |
| 843 | |
| 844 | return 0; |
| 845 | } |
| 846 | EXPORT_SYMBOL_GPL(sst_byt_dsp_boot); |
| 847 | |
| 848 | int sst_byt_dsp_wait_for_ready(struct device *dev, struct sst_pdata *pdata) |
| 849 | { |
| 850 | struct sst_byt *byt = pdata->dsp; |
| 851 | int err; |
| 852 | |
| 853 | dev_dbg(byt->dev, "wait for dsp reboot\n"); |
| 854 | |
| 855 | err = wait_event_timeout(byt->boot_wait, byt->boot_complete, |
| 856 | msecs_to_jiffies(IPC_BOOT_MSECS)); |
| 857 | if (err == 0) { |
| 858 | dev_err(byt->dev, "ipc: error DSP boot timeout\n"); |
| 859 | return -EIO; |
| 860 | } |
| 861 | |
| 862 | dev_dbg(byt->dev, "dsp rebooted\n"); |
| 863 | return 0; |
| 864 | } |
| 865 | EXPORT_SYMBOL_GPL(sst_byt_dsp_wait_for_ready); |
| 866 | |
Mark Brown | a4b1299 | 2014-03-12 23:04:35 +0000 | [diff] [blame] | 867 | int sst_byt_dsp_init(struct device *dev, struct sst_pdata *pdata) |
| 868 | { |
| 869 | struct sst_byt *byt; |
| 870 | struct sst_fw *byt_sst_fw; |
| 871 | int err; |
| 872 | |
| 873 | dev_dbg(dev, "initialising Byt DSP IPC\n"); |
| 874 | |
| 875 | byt = devm_kzalloc(dev, sizeof(*byt), GFP_KERNEL); |
| 876 | if (byt == NULL) |
| 877 | return -ENOMEM; |
| 878 | |
| 879 | byt->dev = dev; |
| 880 | INIT_LIST_HEAD(&byt->stream_list); |
| 881 | INIT_LIST_HEAD(&byt->tx_list); |
| 882 | INIT_LIST_HEAD(&byt->rx_list); |
| 883 | INIT_LIST_HEAD(&byt->empty_list); |
| 884 | init_waitqueue_head(&byt->boot_wait); |
| 885 | init_waitqueue_head(&byt->wait_txq); |
| 886 | |
| 887 | err = msg_empty_list_init(byt); |
| 888 | if (err < 0) |
| 889 | return -ENOMEM; |
| 890 | |
| 891 | /* start the IPC message thread */ |
| 892 | init_kthread_worker(&byt->kworker); |
| 893 | byt->tx_thread = kthread_run(kthread_worker_fn, |
Kees Cook | 3538632 | 2014-05-22 11:43:55 -0700 | [diff] [blame] | 894 | &byt->kworker, "%s", |
Mark Brown | a4b1299 | 2014-03-12 23:04:35 +0000 | [diff] [blame] | 895 | dev_name(byt->dev)); |
| 896 | if (IS_ERR(byt->tx_thread)) { |
| 897 | err = PTR_ERR(byt->tx_thread); |
| 898 | dev_err(byt->dev, "error failed to create message TX task\n"); |
| 899 | goto err_free_msg; |
| 900 | } |
| 901 | init_kthread_work(&byt->kwork, sst_byt_ipc_tx_msgs); |
| 902 | |
| 903 | byt_dev.thread_context = byt; |
| 904 | |
| 905 | /* init SST shim */ |
| 906 | byt->dsp = sst_dsp_new(dev, &byt_dev, pdata); |
| 907 | if (byt->dsp == NULL) { |
| 908 | err = -ENODEV; |
Imre Deak | 9cf0e45 | 2014-05-30 10:52:29 +0300 | [diff] [blame] | 909 | goto dsp_err; |
Mark Brown | a4b1299 | 2014-03-12 23:04:35 +0000 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | /* keep the DSP in reset state for base FW loading */ |
| 913 | sst_dsp_reset(byt->dsp); |
| 914 | |
| 915 | byt_sst_fw = sst_fw_new(byt->dsp, pdata->fw, byt); |
| 916 | if (byt_sst_fw == NULL) { |
| 917 | err = -ENODEV; |
| 918 | dev_err(dev, "error: failed to load firmware\n"); |
| 919 | goto fw_err; |
| 920 | } |
| 921 | |
| 922 | /* wait for DSP boot completion */ |
| 923 | sst_dsp_boot(byt->dsp); |
| 924 | err = wait_event_timeout(byt->boot_wait, byt->boot_complete, |
| 925 | msecs_to_jiffies(IPC_BOOT_MSECS)); |
| 926 | if (err == 0) { |
| 927 | err = -EIO; |
| 928 | dev_err(byt->dev, "ipc: error DSP boot timeout\n"); |
| 929 | goto boot_err; |
| 930 | } |
| 931 | |
| 932 | pdata->dsp = byt; |
Liam Girdwood | af94aa5 | 2014-05-08 16:07:26 +0300 | [diff] [blame] | 933 | byt->fw = byt_sst_fw; |
Mark Brown | a4b1299 | 2014-03-12 23:04:35 +0000 | [diff] [blame] | 934 | |
| 935 | return 0; |
| 936 | |
| 937 | boot_err: |
| 938 | sst_dsp_reset(byt->dsp); |
| 939 | sst_fw_free(byt_sst_fw); |
| 940 | fw_err: |
| 941 | sst_dsp_free(byt->dsp); |
Imre Deak | 9cf0e45 | 2014-05-30 10:52:29 +0300 | [diff] [blame] | 942 | dsp_err: |
| 943 | kthread_stop(byt->tx_thread); |
Mark Brown | a4b1299 | 2014-03-12 23:04:35 +0000 | [diff] [blame] | 944 | err_free_msg: |
| 945 | kfree(byt->msg); |
| 946 | |
| 947 | return err; |
| 948 | } |
| 949 | EXPORT_SYMBOL_GPL(sst_byt_dsp_init); |
| 950 | |
| 951 | void sst_byt_dsp_free(struct device *dev, struct sst_pdata *pdata) |
| 952 | { |
| 953 | struct sst_byt *byt = pdata->dsp; |
| 954 | |
| 955 | sst_dsp_reset(byt->dsp); |
| 956 | sst_fw_free_all(byt->dsp); |
| 957 | sst_dsp_free(byt->dsp); |
Imre Deak | 9cf0e45 | 2014-05-30 10:52:29 +0300 | [diff] [blame] | 958 | kthread_stop(byt->tx_thread); |
Mark Brown | a4b1299 | 2014-03-12 23:04:35 +0000 | [diff] [blame] | 959 | kfree(byt->msg); |
| 960 | } |
| 961 | EXPORT_SYMBOL_GPL(sst_byt_dsp_free); |