diff options
author | Jarkko Nikula <jarkko.nikula@linux.intel.com> | 2014-02-24 10:26:58 -0500 |
---|---|---|
committer | Mark Brown <broonie@linaro.org> | 2014-02-24 23:44:34 -0500 |
commit | f7d01fd6754c1a257af46ec465d946132c7d004d (patch) | |
tree | 7a2267a9f0d71f53cc8472cfb3464909e66f5318 | |
parent | f746966377d08aea60a1f21a6387855409524b9b (diff) |
ASoC: Intel: Add Intel Baytrail SST DSP IPC support
Add support for Baytrail SST DSP IPC. This provides mechanism to communicate
with the DSP firmware.
Signed-off-by: Jarkko Nikula <jarkko.nikula@linux.intel.com>
Acked-by: Liam Girdwood <liam.r.girdwood@intel.com>
Signed-off-by: Mark Brown <broonie@linaro.org>
-rw-r--r-- | sound/soc/intel/sst-baytrail-ipc.c | 866 | ||||
-rw-r--r-- | sound/soc/intel/sst-baytrail-ipc.h | 69 |
2 files changed, 935 insertions, 0 deletions
diff --git a/sound/soc/intel/sst-baytrail-ipc.c b/sound/soc/intel/sst-baytrail-ipc.c new file mode 100644 index 000000000000..8c91a68b90bc --- /dev/null +++ b/sound/soc/intel/sst-baytrail-ipc.c | |||
@@ -0,0 +1,866 @@ | |||
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> | ||
25 | #include <linux/list.h> | ||
26 | #include <linux/platform_device.h> | ||
27 | #include <linux/kthread.h> | ||
28 | #include <linux/firmware.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; | ||
175 | |||
176 | /* IPC messaging */ | ||
177 | struct list_head tx_list; | ||
178 | struct list_head rx_list; | ||
179 | struct list_head empty_list; | ||
180 | wait_queue_head_t wait_txq; | ||
181 | struct task_struct *tx_thread; | ||
182 | struct kthread_worker kworker; | ||
183 | struct kthread_work kwork; | ||
184 | struct ipc_message *msg; | ||
185 | }; | ||
186 | |||
187 | static inline u64 sst_byt_header(int msg_id, int data, bool large, int str_id) | ||
188 | { | ||
189 | u64 header; | ||
190 | |||
191 | header = IPC_HEADER_MSG_ID(msg_id) | | ||
192 | IPC_HEADER_STR_ID(str_id) | | ||
193 | IPC_HEADER_LARGE(large) | | ||
194 | IPC_HEADER_DATA(data) | | ||
195 | SST_BYT_IPCX_BUSY; | ||
196 | |||
197 | return header; | ||
198 | } | ||
199 | |||
200 | static inline u16 sst_byt_header_msg_id(u64 header) | ||
201 | { | ||
202 | return header & IPC_HEADER_MSG_ID_MASK; | ||
203 | } | ||
204 | |||
205 | static inline u8 sst_byt_header_str_id(u64 header) | ||
206 | { | ||
207 | return (header >> IPC_HEADER_STR_ID_SHIFT) & IPC_HEADER_STR_ID_MASK; | ||
208 | } | ||
209 | |||
210 | static inline u16 sst_byt_header_data(u64 header) | ||
211 | { | ||
212 | return (header >> IPC_HEADER_DATA_SHIFT) & IPC_HEADER_DATA_MASK; | ||
213 | } | ||
214 | |||
215 | static struct sst_byt_stream *sst_byt_get_stream(struct sst_byt *byt, | ||
216 | int stream_id) | ||
217 | { | ||
218 | struct sst_byt_stream *stream; | ||
219 | |||
220 | list_for_each_entry(stream, &byt->stream_list, node) { | ||
221 | if (stream->str_id == stream_id) | ||
222 | return stream; | ||
223 | } | ||
224 | |||
225 | return NULL; | ||
226 | } | ||
227 | |||
228 | static void sst_byt_ipc_shim_dbg(struct sst_byt *byt, const char *text) | ||
229 | { | ||
230 | struct sst_dsp *sst = byt->dsp; | ||
231 | u64 isr, ipcd, imrx, ipcx; | ||
232 | |||
233 | ipcx = sst_dsp_shim_read64_unlocked(sst, SST_IPCX); | ||
234 | isr = sst_dsp_shim_read64_unlocked(sst, SST_ISRX); | ||
235 | ipcd = sst_dsp_shim_read64_unlocked(sst, SST_IPCD); | ||
236 | imrx = sst_dsp_shim_read64_unlocked(sst, SST_IMRX); | ||
237 | |||
238 | dev_err(byt->dev, | ||
239 | "ipc: --%s-- ipcx 0x%llx isr 0x%llx ipcd 0x%llx imrx 0x%llx\n", | ||
240 | text, ipcx, isr, ipcd, imrx); | ||
241 | } | ||
242 | |||
243 | /* locks held by caller */ | ||
244 | static struct ipc_message *sst_byt_msg_get_empty(struct sst_byt *byt) | ||
245 | { | ||
246 | struct ipc_message *msg = NULL; | ||
247 | |||
248 | if (!list_empty(&byt->empty_list)) { | ||
249 | msg = list_first_entry(&byt->empty_list, | ||
250 | struct ipc_message, list); | ||
251 | list_del(&msg->list); | ||
252 | } | ||
253 | |||
254 | return msg; | ||
255 | } | ||
256 | |||
257 | static void sst_byt_ipc_tx_msgs(struct kthread_work *work) | ||
258 | { | ||
259 | struct sst_byt *byt = | ||
260 | container_of(work, struct sst_byt, kwork); | ||
261 | struct ipc_message *msg; | ||
262 | u64 ipcx; | ||
263 | unsigned long flags; | ||
264 | |||
265 | spin_lock_irqsave(&byt->dsp->spinlock, flags); | ||
266 | if (list_empty(&byt->tx_list)) { | ||
267 | spin_unlock_irqrestore(&byt->dsp->spinlock, flags); | ||
268 | return; | ||
269 | } | ||
270 | |||
271 | /* if the DSP is busy we will TX messages after IRQ */ | ||
272 | ipcx = sst_dsp_shim_read64_unlocked(byt->dsp, SST_IPCX); | ||
273 | if (ipcx & SST_BYT_IPCX_BUSY) { | ||
274 | spin_unlock_irqrestore(&byt->dsp->spinlock, flags); | ||
275 | return; | ||
276 | } | ||
277 | |||
278 | msg = list_first_entry(&byt->tx_list, struct ipc_message, list); | ||
279 | |||
280 | list_move(&msg->list, &byt->rx_list); | ||
281 | |||
282 | /* send the message */ | ||
283 | if (msg->header & IPC_HEADER_LARGE(true)) | ||
284 | sst_dsp_outbox_write(byt->dsp, msg->tx_data, msg->tx_size); | ||
285 | sst_dsp_shim_write64_unlocked(byt->dsp, SST_IPCX, msg->header); | ||
286 | |||
287 | spin_unlock_irqrestore(&byt->dsp->spinlock, flags); | ||
288 | } | ||
289 | |||
290 | static inline void sst_byt_tx_msg_reply_complete(struct sst_byt *byt, | ||
291 | struct ipc_message *msg) | ||
292 | { | ||
293 | msg->complete = true; | ||
294 | |||
295 | if (!msg->wait) | ||
296 | list_add_tail(&msg->list, &byt->empty_list); | ||
297 | else | ||
298 | wake_up(&msg->waitq); | ||
299 | } | ||
300 | |||
301 | static int sst_byt_tx_wait_done(struct sst_byt *byt, struct ipc_message *msg, | ||
302 | void *rx_data) | ||
303 | { | ||
304 | unsigned long flags; | ||
305 | int ret; | ||
306 | |||
307 | /* wait for DSP completion */ | ||
308 | ret = wait_event_timeout(msg->waitq, msg->complete, | ||
309 | msecs_to_jiffies(IPC_TIMEOUT_MSECS)); | ||
310 | |||
311 | spin_lock_irqsave(&byt->dsp->spinlock, flags); | ||
312 | if (ret == 0) { | ||
313 | list_del(&msg->list); | ||
314 | sst_byt_ipc_shim_dbg(byt, "message timeout"); | ||
315 | |||
316 | ret = -ETIMEDOUT; | ||
317 | } else { | ||
318 | |||
319 | /* copy the data returned from DSP */ | ||
320 | if (msg->rx_size) | ||
321 | memcpy(rx_data, msg->rx_data, msg->rx_size); | ||
322 | ret = msg->errno; | ||
323 | } | ||
324 | |||
325 | list_add_tail(&msg->list, &byt->empty_list); | ||
326 | spin_unlock_irqrestore(&byt->dsp->spinlock, flags); | ||
327 | return ret; | ||
328 | } | ||
329 | |||
330 | static int sst_byt_ipc_tx_message(struct sst_byt *byt, u64 header, | ||
331 | void *tx_data, size_t tx_bytes, | ||
332 | void *rx_data, size_t rx_bytes, int wait) | ||
333 | { | ||
334 | unsigned long flags; | ||
335 | struct ipc_message *msg; | ||
336 | |||
337 | spin_lock_irqsave(&byt->dsp->spinlock, flags); | ||
338 | |||
339 | msg = sst_byt_msg_get_empty(byt); | ||
340 | if (msg == NULL) { | ||
341 | spin_unlock_irqrestore(&byt->dsp->spinlock, flags); | ||
342 | return -EBUSY; | ||
343 | } | ||
344 | |||
345 | msg->header = header; | ||
346 | msg->tx_size = tx_bytes; | ||
347 | msg->rx_size = rx_bytes; | ||
348 | msg->wait = wait; | ||
349 | msg->errno = 0; | ||
350 | msg->complete = false; | ||
351 | |||
352 | if (tx_bytes) { | ||
353 | /* msg content = lower 32-bit of the header + data */ | ||
354 | *(u32 *)msg->tx_data = (u32)(header & (u32)-1); | ||
355 | memcpy(msg->tx_data + sizeof(u32), tx_data, tx_bytes); | ||
356 | msg->tx_size += sizeof(u32); | ||
357 | } | ||
358 | |||
359 | list_add_tail(&msg->list, &byt->tx_list); | ||
360 | spin_unlock_irqrestore(&byt->dsp->spinlock, flags); | ||
361 | |||
362 | queue_kthread_work(&byt->kworker, &byt->kwork); | ||
363 | |||
364 | if (wait) | ||
365 | return sst_byt_tx_wait_done(byt, msg, rx_data); | ||
366 | else | ||
367 | return 0; | ||
368 | } | ||
369 | |||
370 | static inline int sst_byt_ipc_tx_msg_wait(struct sst_byt *byt, u64 header, | ||
371 | void *tx_data, size_t tx_bytes, | ||
372 | void *rx_data, size_t rx_bytes) | ||
373 | { | ||
374 | return sst_byt_ipc_tx_message(byt, header, tx_data, tx_bytes, | ||
375 | rx_data, rx_bytes, 1); | ||
376 | } | ||
377 | |||
378 | static inline int sst_byt_ipc_tx_msg_nowait(struct sst_byt *byt, u64 header, | ||
379 | void *tx_data, size_t tx_bytes) | ||
380 | { | ||
381 | return sst_byt_ipc_tx_message(byt, header, tx_data, tx_bytes, | ||
382 | NULL, 0, 0); | ||
383 | } | ||
384 | |||
385 | static struct ipc_message *sst_byt_reply_find_msg(struct sst_byt *byt, | ||
386 | u64 header) | ||
387 | { | ||
388 | struct ipc_message *msg = NULL, *_msg; | ||
389 | u64 mask; | ||
390 | |||
391 | /* match reply to message sent based on msg and stream IDs */ | ||
392 | mask = IPC_HEADER_MSG_ID_MASK | | ||
393 | IPC_HEADER_STR_ID_MASK << IPC_HEADER_STR_ID_SHIFT; | ||
394 | header &= mask; | ||
395 | |||
396 | if (list_empty(&byt->rx_list)) { | ||
397 | dev_err(byt->dev, | ||
398 | "ipc: rx list is empty but received 0x%llx\n", header); | ||
399 | goto out; | ||
400 | } | ||
401 | |||
402 | list_for_each_entry(_msg, &byt->rx_list, list) { | ||
403 | if ((_msg->header & mask) == header) { | ||
404 | msg = _msg; | ||
405 | break; | ||
406 | } | ||
407 | } | ||
408 | |||
409 | out: | ||
410 | return msg; | ||
411 | } | ||
412 | |||
413 | static void sst_byt_stream_update(struct sst_byt *byt, struct ipc_message *msg) | ||
414 | { | ||
415 | struct sst_byt_stream *stream; | ||
416 | u64 header = msg->header; | ||
417 | u8 stream_id = sst_byt_header_str_id(header); | ||
418 | u8 stream_msg = sst_byt_header_msg_id(header); | ||
419 | |||
420 | stream = sst_byt_get_stream(byt, stream_id); | ||
421 | if (stream == NULL) | ||
422 | return; | ||
423 | |||
424 | switch (stream_msg) { | ||
425 | case IPC_IA_DROP_STREAM: | ||
426 | case IPC_IA_PAUSE_STREAM: | ||
427 | case IPC_IA_FREE_STREAM: | ||
428 | stream->running = false; | ||
429 | break; | ||
430 | case IPC_IA_START_STREAM: | ||
431 | case IPC_IA_RESUME_STREAM: | ||
432 | stream->running = true; | ||
433 | break; | ||
434 | } | ||
435 | } | ||
436 | |||
437 | static int sst_byt_process_reply(struct sst_byt *byt, u64 header) | ||
438 | { | ||
439 | struct ipc_message *msg; | ||
440 | |||
441 | msg = sst_byt_reply_find_msg(byt, header); | ||
442 | if (msg == NULL) | ||
443 | return 1; | ||
444 | |||
445 | if (header & IPC_HEADER_LARGE(true)) { | ||
446 | msg->rx_size = sst_byt_header_data(header); | ||
447 | sst_dsp_inbox_read(byt->dsp, msg->rx_data, msg->rx_size); | ||
448 | } | ||
449 | |||
450 | /* update any stream states */ | ||
451 | sst_byt_stream_update(byt, msg); | ||
452 | |||
453 | list_del(&msg->list); | ||
454 | /* wake up */ | ||
455 | sst_byt_tx_msg_reply_complete(byt, msg); | ||
456 | |||
457 | return 1; | ||
458 | } | ||
459 | |||
460 | static void sst_byt_fw_ready(struct sst_byt *byt, u64 header) | ||
461 | { | ||
462 | dev_dbg(byt->dev, "ipc: DSP is ready 0x%llX\n", header); | ||
463 | |||
464 | byt->boot_complete = true; | ||
465 | wake_up(&byt->boot_wait); | ||
466 | } | ||
467 | |||
468 | static int sst_byt_process_notification(struct sst_byt *byt, | ||
469 | unsigned long *flags) | ||
470 | { | ||
471 | struct sst_dsp *sst = byt->dsp; | ||
472 | struct sst_byt_stream *stream; | ||
473 | u64 header; | ||
474 | u8 msg_id, stream_id; | ||
475 | int handled = 1; | ||
476 | |||
477 | header = sst_dsp_shim_read64_unlocked(sst, SST_IPCD); | ||
478 | msg_id = sst_byt_header_msg_id(header); | ||
479 | |||
480 | switch (msg_id) { | ||
481 | case IPC_SST_PERIOD_ELAPSED: | ||
482 | stream_id = sst_byt_header_str_id(header); | ||
483 | stream = sst_byt_get_stream(byt, stream_id); | ||
484 | if (stream && stream->running && stream->notify_position) { | ||
485 | spin_unlock_irqrestore(&sst->spinlock, *flags); | ||
486 | stream->notify_position(stream, stream->pdata); | ||
487 | spin_lock_irqsave(&sst->spinlock, *flags); | ||
488 | } | ||
489 | break; | ||
490 | case IPC_IA_FW_INIT_CMPLT: | ||
491 | sst_byt_fw_ready(byt, header); | ||
492 | break; | ||
493 | } | ||
494 | |||
495 | return handled; | ||
496 | } | ||
497 | |||
498 | static irqreturn_t sst_byt_irq_thread(int irq, void *context) | ||
499 | { | ||
500 | struct sst_dsp *sst = (struct sst_dsp *) context; | ||
501 | struct sst_byt *byt = sst_dsp_get_thread_context(sst); | ||
502 | u64 header; | ||
503 | unsigned long flags; | ||
504 | |||
505 | spin_lock_irqsave(&sst->spinlock, flags); | ||
506 | |||
507 | header = sst_dsp_shim_read64_unlocked(sst, SST_IPCD); | ||
508 | if (header & SST_BYT_IPCD_BUSY) { | ||
509 | if (header & IPC_NOTIFICATION) { | ||
510 | /* message from ADSP */ | ||
511 | sst_byt_process_notification(byt, &flags); | ||
512 | } else { | ||
513 | /* reply from ADSP */ | ||
514 | sst_byt_process_reply(byt, header); | ||
515 | } | ||
516 | /* | ||
517 | * clear IPCD BUSY bit and set DONE bit. Tell DSP we have | ||
518 | * processed the message and can accept new. Clear data part | ||
519 | * of the header | ||
520 | */ | ||
521 | sst_dsp_shim_update_bits64_unlocked(sst, SST_IPCD, | ||
522 | SST_BYT_IPCD_DONE | SST_BYT_IPCD_BUSY | | ||
523 | IPC_HEADER_DATA(IPC_HEADER_DATA_MASK), | ||
524 | SST_BYT_IPCD_DONE); | ||
525 | /* unmask message request interrupts */ | ||
526 | sst_dsp_shim_update_bits64_unlocked(sst, SST_IMRX, | ||
527 | SST_BYT_IMRX_REQUEST, 0); | ||
528 | } | ||
529 | |||
530 | spin_unlock_irqrestore(&sst->spinlock, flags); | ||
531 | |||
532 | /* continue to send any remaining messages... */ | ||
533 | queue_kthread_work(&byt->kworker, &byt->kwork); | ||
534 | |||
535 | return IRQ_HANDLED; | ||
536 | } | ||
537 | |||
538 | /* stream API */ | ||
539 | struct sst_byt_stream *sst_byt_stream_new(struct sst_byt *byt, int id, | ||
540 | u32 (*notify_position)(struct sst_byt_stream *stream, void *data), | ||
541 | void *data) | ||
542 | { | ||
543 | struct sst_byt_stream *stream; | ||
544 | |||
545 | stream = kzalloc(sizeof(*stream), GFP_KERNEL); | ||
546 | if (stream == NULL) | ||
547 | return NULL; | ||
548 | |||
549 | list_add(&stream->node, &byt->stream_list); | ||
550 | stream->notify_position = notify_position; | ||
551 | stream->pdata = data; | ||
552 | stream->byt = byt; | ||
553 | stream->str_id = id; | ||
554 | |||
555 | return stream; | ||
556 | } | ||
557 | |||
558 | int sst_byt_stream_set_bits(struct sst_byt *byt, struct sst_byt_stream *stream, | ||
559 | int bits) | ||
560 | { | ||
561 | stream->request.pcm_params.pcm_wd_sz = bits; | ||
562 | return 0; | ||
563 | } | ||
564 | |||
565 | int sst_byt_stream_set_channels(struct sst_byt *byt, | ||
566 | struct sst_byt_stream *stream, u8 channels) | ||
567 | { | ||
568 | stream->request.pcm_params.num_chan = channels; | ||
569 | return 0; | ||
570 | } | ||
571 | |||
572 | int sst_byt_stream_set_rate(struct sst_byt *byt, struct sst_byt_stream *stream, | ||
573 | unsigned int rate) | ||
574 | { | ||
575 | stream->request.pcm_params.sfreq = rate; | ||
576 | return 0; | ||
577 | } | ||
578 | |||
579 | /* stream sonfiguration */ | ||
580 | int sst_byt_stream_type(struct sst_byt *byt, struct sst_byt_stream *stream, | ||
581 | int codec_type, int stream_type, int operation) | ||
582 | { | ||
583 | stream->request.str_type.codec_type = codec_type; | ||
584 | stream->request.str_type.str_type = stream_type; | ||
585 | stream->request.str_type.operation = operation; | ||
586 | stream->request.str_type.time_slots = 0xc; | ||
587 | |||
588 | return 0; | ||
589 | } | ||
590 | |||
591 | int sst_byt_stream_buffer(struct sst_byt *byt, struct sst_byt_stream *stream, | ||
592 | uint32_t buffer_addr, uint32_t buffer_size) | ||
593 | { | ||
594 | stream->request.frame_info.num_entries = 1; | ||
595 | stream->request.frame_info.ring_buf_info[0].addr = buffer_addr; | ||
596 | stream->request.frame_info.ring_buf_info[0].size = buffer_size; | ||
597 | /* calculate bytes per 4 ms fragment */ | ||
598 | stream->request.frame_info.frag_size = | ||
599 | stream->request.pcm_params.sfreq * | ||
600 | stream->request.pcm_params.num_chan * | ||
601 | stream->request.pcm_params.pcm_wd_sz / 8 * | ||
602 | 4 / 1000; | ||
603 | return 0; | ||
604 | } | ||
605 | |||
606 | int sst_byt_stream_commit(struct sst_byt *byt, struct sst_byt_stream *stream) | ||
607 | { | ||
608 | struct sst_byt_alloc_params *str_req = &stream->request; | ||
609 | struct sst_byt_alloc_response *reply = &stream->reply; | ||
610 | u64 header; | ||
611 | int ret; | ||
612 | |||
613 | header = sst_byt_header(IPC_IA_ALLOC_STREAM, | ||
614 | sizeof(*str_req) + sizeof(u32), | ||
615 | true, stream->str_id); | ||
616 | ret = sst_byt_ipc_tx_msg_wait(byt, header, str_req, sizeof(*str_req), | ||
617 | reply, sizeof(*reply)); | ||
618 | if (ret < 0) { | ||
619 | dev_err(byt->dev, "ipc: error stream commit failed\n"); | ||
620 | return ret; | ||
621 | } | ||
622 | |||
623 | stream->commited = true; | ||
624 | |||
625 | return 0; | ||
626 | } | ||
627 | |||
628 | int sst_byt_stream_free(struct sst_byt *byt, struct sst_byt_stream *stream) | ||
629 | { | ||
630 | u64 header; | ||
631 | int ret = 0; | ||
632 | |||
633 | if (!stream->commited) | ||
634 | goto out; | ||
635 | |||
636 | header = sst_byt_header(IPC_IA_FREE_STREAM, 0, false, stream->str_id); | ||
637 | ret = sst_byt_ipc_tx_msg_wait(byt, header, NULL, 0, NULL, 0); | ||
638 | if (ret < 0) { | ||
639 | dev_err(byt->dev, "ipc: free stream %d failed\n", | ||
640 | stream->str_id); | ||
641 | return -EAGAIN; | ||
642 | } | ||
643 | |||
644 | stream->commited = false; | ||
645 | out: | ||
646 | list_del(&stream->node); | ||
647 | kfree(stream); | ||
648 | |||
649 | return ret; | ||
650 | } | ||
651 | |||
652 | static int sst_byt_stream_operations(struct sst_byt *byt, int type, | ||
653 | int stream_id, int wait) | ||
654 | { | ||
655 | struct sst_byt_start_stream_params start_stream; | ||
656 | u64 header; | ||
657 | void *tx_msg = NULL; | ||
658 | size_t size = 0; | ||
659 | |||
660 | if (type != IPC_IA_START_STREAM) { | ||
661 | header = sst_byt_header(type, 0, false, stream_id); | ||
662 | } else { | ||
663 | start_stream.byte_offset = 0; | ||
664 | header = sst_byt_header(IPC_IA_START_STREAM, | ||
665 | sizeof(start_stream) + sizeof(u32), | ||
666 | true, stream_id); | ||
667 | tx_msg = &start_stream; | ||
668 | size = sizeof(start_stream); | ||
669 | } | ||
670 | |||
671 | if (wait) | ||
672 | return sst_byt_ipc_tx_msg_wait(byt, header, | ||
673 | tx_msg, size, NULL, 0); | ||
674 | else | ||
675 | return sst_byt_ipc_tx_msg_nowait(byt, header, tx_msg, size); | ||
676 | } | ||
677 | |||
678 | /* stream ALSA trigger operations */ | ||
679 | int sst_byt_stream_start(struct sst_byt *byt, struct sst_byt_stream *stream) | ||
680 | { | ||
681 | int ret; | ||
682 | |||
683 | ret = sst_byt_stream_operations(byt, IPC_IA_START_STREAM, | ||
684 | stream->str_id, 0); | ||
685 | if (ret < 0) | ||
686 | dev_err(byt->dev, "ipc: error failed to start stream %d\n", | ||
687 | stream->str_id); | ||
688 | |||
689 | return ret; | ||
690 | } | ||
691 | |||
692 | int sst_byt_stream_stop(struct sst_byt *byt, struct sst_byt_stream *stream) | ||
693 | { | ||
694 | int ret; | ||
695 | |||
696 | /* don't stop streams that are not commited */ | ||
697 | if (!stream->commited) | ||
698 | return 0; | ||
699 | |||
700 | ret = sst_byt_stream_operations(byt, IPC_IA_DROP_STREAM, | ||
701 | stream->str_id, 0); | ||
702 | if (ret < 0) | ||
703 | dev_err(byt->dev, "ipc: error failed to stop stream %d\n", | ||
704 | stream->str_id); | ||
705 | return ret; | ||
706 | } | ||
707 | |||
708 | int sst_byt_stream_pause(struct sst_byt *byt, struct sst_byt_stream *stream) | ||
709 | { | ||
710 | int ret; | ||
711 | |||
712 | ret = sst_byt_stream_operations(byt, IPC_IA_PAUSE_STREAM, | ||
713 | stream->str_id, 0); | ||
714 | if (ret < 0) | ||
715 | dev_err(byt->dev, "ipc: error failed to pause stream %d\n", | ||
716 | stream->str_id); | ||
717 | |||
718 | return ret; | ||
719 | } | ||
720 | |||
721 | int sst_byt_stream_resume(struct sst_byt *byt, struct sst_byt_stream *stream) | ||
722 | { | ||
723 | int ret; | ||
724 | |||
725 | ret = sst_byt_stream_operations(byt, IPC_IA_RESUME_STREAM, | ||
726 | stream->str_id, 0); | ||
727 | if (ret < 0) | ||
728 | dev_err(byt->dev, "ipc: error failed to resume stream %d\n", | ||
729 | stream->str_id); | ||
730 | |||
731 | return ret; | ||
732 | } | ||
733 | |||
734 | int sst_byt_get_dsp_position(struct sst_byt *byt, | ||
735 | struct sst_byt_stream *stream, int buffer_size) | ||
736 | { | ||
737 | struct sst_dsp *sst = byt->dsp; | ||
738 | struct sst_byt_tstamp fw_tstamp; | ||
739 | u8 str_id = stream->str_id; | ||
740 | u32 tstamp_offset; | ||
741 | |||
742 | tstamp_offset = SST_BYT_TIMESTAMP_OFFSET + str_id * sizeof(fw_tstamp); | ||
743 | memcpy_fromio(&fw_tstamp, | ||
744 | sst->addr.lpe + tstamp_offset, sizeof(fw_tstamp)); | ||
745 | |||
746 | return do_div(fw_tstamp.ring_buffer_counter, buffer_size); | ||
747 | } | ||
748 | |||
749 | static int msg_empty_list_init(struct sst_byt *byt) | ||
750 | { | ||
751 | struct ipc_message *msg; | ||
752 | int i; | ||
753 | |||
754 | byt->msg = kzalloc(sizeof(*msg) * IPC_EMPTY_LIST_SIZE, GFP_KERNEL); | ||
755 | if (byt->msg == NULL) | ||
756 | return -ENOMEM; | ||
757 | |||
758 | for (i = 0; i < IPC_EMPTY_LIST_SIZE; i++) { | ||
759 | init_waitqueue_head(&byt->msg[i].waitq); | ||
760 | list_add(&byt->msg[i].list, &byt->empty_list); | ||
761 | } | ||
762 | |||
763 | return 0; | ||
764 | } | ||
765 | |||
766 | struct sst_dsp *sst_byt_get_dsp(struct sst_byt *byt) | ||
767 | { | ||
768 | return byt->dsp; | ||
769 | } | ||
770 | |||
771 | static struct sst_dsp_device byt_dev = { | ||
772 | .thread = sst_byt_irq_thread, | ||
773 | .ops = &sst_byt_ops, | ||
774 | }; | ||
775 | |||
776 | int sst_byt_dsp_init(struct device *dev, struct sst_pdata *pdata) | ||
777 | { | ||
778 | struct sst_byt *byt; | ||
779 | struct sst_fw *byt_sst_fw; | ||
780 | int err; | ||
781 | |||
782 | dev_dbg(dev, "initialising Byt DSP IPC\n"); | ||
783 | |||
784 | byt = devm_kzalloc(dev, sizeof(*byt), GFP_KERNEL); | ||
785 | if (byt == NULL) | ||
786 | return -ENOMEM; | ||
787 | |||
788 | byt->dev = dev; | ||
789 | INIT_LIST_HEAD(&byt->stream_list); | ||
790 | INIT_LIST_HEAD(&byt->tx_list); | ||
791 | INIT_LIST_HEAD(&byt->rx_list); | ||
792 | INIT_LIST_HEAD(&byt->empty_list); | ||
793 | init_waitqueue_head(&byt->boot_wait); | ||
794 | init_waitqueue_head(&byt->wait_txq); | ||
795 | |||
796 | err = msg_empty_list_init(byt); | ||
797 | if (err < 0) | ||
798 | goto list_err; | ||
799 | |||
800 | /* start the IPC message thread */ | ||
801 | init_kthread_worker(&byt->kworker); | ||
802 | byt->tx_thread = kthread_run(kthread_worker_fn, | ||
803 | &byt->kworker, | ||
804 | dev_name(byt->dev)); | ||
805 | if (IS_ERR(byt->tx_thread)) { | ||
806 | err = PTR_ERR(byt->tx_thread); | ||
807 | dev_err(byt->dev, "error failed to create message TX task\n"); | ||
808 | goto list_err; | ||
809 | } | ||
810 | init_kthread_work(&byt->kwork, sst_byt_ipc_tx_msgs); | ||
811 | |||
812 | byt_dev.thread_context = byt; | ||
813 | |||
814 | /* init SST shim */ | ||
815 | byt->dsp = sst_dsp_new(dev, &byt_dev, pdata); | ||
816 | if (byt->dsp == NULL) { | ||
817 | err = -ENODEV; | ||
818 | goto list_err; | ||
819 | } | ||
820 | |||
821 | /* keep the DSP in reset state for base FW loading */ | ||
822 | sst_dsp_reset(byt->dsp); | ||
823 | |||
824 | byt_sst_fw = sst_fw_new(byt->dsp, pdata->fw, byt); | ||
825 | if (byt_sst_fw == NULL) { | ||
826 | err = -ENODEV; | ||
827 | dev_err(dev, "error: failed to load firmware\n"); | ||
828 | goto fw_err; | ||
829 | } | ||
830 | |||
831 | /* wait for DSP boot completion */ | ||
832 | sst_dsp_boot(byt->dsp); | ||
833 | err = wait_event_timeout(byt->boot_wait, byt->boot_complete, | ||
834 | msecs_to_jiffies(IPC_BOOT_MSECS)); | ||
835 | if (err == 0) { | ||
836 | err = -EIO; | ||
837 | dev_err(byt->dev, "ipc: error DSP boot timeout\n"); | ||
838 | goto boot_err; | ||
839 | } | ||
840 | |||
841 | pdata->dsp = byt; | ||
842 | |||
843 | return 0; | ||
844 | |||
845 | boot_err: | ||
846 | sst_dsp_reset(byt->dsp); | ||
847 | sst_fw_free(byt_sst_fw); | ||
848 | fw_err: | ||
849 | sst_dsp_free(byt->dsp); | ||
850 | kfree(byt->msg); | ||
851 | list_err: | ||
852 | kfree(byt); | ||
853 | return err; | ||
854 | } | ||
855 | EXPORT_SYMBOL_GPL(sst_byt_dsp_init); | ||
856 | |||
857 | void sst_byt_dsp_free(struct device *dev, struct sst_pdata *pdata) | ||
858 | { | ||
859 | struct sst_byt *byt = pdata->dsp; | ||
860 | |||
861 | sst_dsp_reset(byt->dsp); | ||
862 | sst_fw_free_all(byt->dsp); | ||
863 | sst_dsp_free(byt->dsp); | ||
864 | kfree(byt->msg); | ||
865 | } | ||
866 | EXPORT_SYMBOL_GPL(sst_byt_dsp_free); | ||
diff --git a/sound/soc/intel/sst-baytrail-ipc.h b/sound/soc/intel/sst-baytrail-ipc.h new file mode 100644 index 000000000000..f172b6440fa9 --- /dev/null +++ b/sound/soc/intel/sst-baytrail-ipc.h | |||
@@ -0,0 +1,69 @@ | |||
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 | #ifndef __SST_BYT_IPC_H | ||
16 | #define __SST_BYT_IPC_H | ||
17 | |||
18 | #include <linux/types.h> | ||
19 | |||
20 | struct sst_byt; | ||
21 | struct sst_byt_stream; | ||
22 | struct sst_pdata; | ||
23 | extern struct sst_ops sst_byt_ops; | ||
24 | |||
25 | |||
26 | #define SST_BYT_MAILBOX_OFFSET 0x144000 | ||
27 | #define SST_BYT_TIMESTAMP_OFFSET (SST_BYT_MAILBOX_OFFSET + 0x800) | ||
28 | |||
29 | /** | ||
30 | * Upfront defined maximum message size that is | ||
31 | * expected by the in/out communication pipes in FW. | ||
32 | */ | ||
33 | #define SST_BYT_IPC_MAX_PAYLOAD_SIZE 200 | ||
34 | |||
35 | /* stream API */ | ||
36 | struct sst_byt_stream *sst_byt_stream_new(struct sst_byt *byt, int id, | ||
37 | uint32_t (*get_write_position)(struct sst_byt_stream *stream, | ||
38 | void *data), | ||
39 | void *data); | ||
40 | |||
41 | /* stream configuration */ | ||
42 | int sst_byt_stream_set_bits(struct sst_byt *byt, struct sst_byt_stream *stream, | ||
43 | int bits); | ||
44 | int sst_byt_stream_set_channels(struct sst_byt *byt, | ||
45 | struct sst_byt_stream *stream, u8 channels); | ||
46 | int sst_byt_stream_set_rate(struct sst_byt *byt, struct sst_byt_stream *stream, | ||
47 | unsigned int rate); | ||
48 | int sst_byt_stream_type(struct sst_byt *byt, struct sst_byt_stream *stream, | ||
49 | int codec_type, int stream_type, int operation); | ||
50 | int sst_byt_stream_buffer(struct sst_byt *byt, struct sst_byt_stream *stream, | ||
51 | uint32_t buffer_addr, uint32_t buffer_size); | ||
52 | int sst_byt_stream_commit(struct sst_byt *byt, struct sst_byt_stream *stream); | ||
53 | int sst_byt_stream_free(struct sst_byt *byt, struct sst_byt_stream *stream); | ||
54 | |||
55 | /* stream ALSA trigger operations */ | ||
56 | int sst_byt_stream_start(struct sst_byt *byt, struct sst_byt_stream *stream); | ||
57 | int sst_byt_stream_stop(struct sst_byt *byt, struct sst_byt_stream *stream); | ||
58 | int sst_byt_stream_pause(struct sst_byt *byt, struct sst_byt_stream *stream); | ||
59 | int sst_byt_stream_resume(struct sst_byt *byt, struct sst_byt_stream *stream); | ||
60 | |||
61 | int sst_byt_get_dsp_position(struct sst_byt *byt, | ||
62 | struct sst_byt_stream *stream, int buffer_size); | ||
63 | |||
64 | /* init */ | ||
65 | int sst_byt_dsp_init(struct device *dev, struct sst_pdata *pdata); | ||
66 | void sst_byt_dsp_free(struct device *dev, struct sst_pdata *pdata); | ||
67 | struct sst_dsp *sst_byt_get_dsp(struct sst_byt *byt); | ||
68 | |||
69 | #endif | ||