Commit | Line | Data |
---|---|---|
a4b12990 MB |
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 <linux/io.h> | |
30 | #include <asm/div64.h> | |
31 | ||
32 | #include "sst-baytrail-ipc.h" | |
33 | #include "sst-dsp.h" | |
34 | #include "sst-dsp-priv.h" | |
35 | ||
36 | /* IPC message timeout */ | |
37 | #define IPC_TIMEOUT_MSECS 300 | |
38 | #define IPC_BOOT_MSECS 200 | |
39 | ||
40 | #define IPC_EMPTY_LIST_SIZE 8 | |
41 | ||
42 | /* IPC header bits */ | |
43 | #define IPC_HEADER_MSG_ID_MASK 0xff | |
44 | #define IPC_HEADER_MSG_ID(x) ((x) & IPC_HEADER_MSG_ID_MASK) | |
45 | #define IPC_HEADER_STR_ID_SHIFT 8 | |
46 | #define IPC_HEADER_STR_ID_MASK 0x1f | |
47 | #define IPC_HEADER_STR_ID(x) (((x) & 0x1f) << IPC_HEADER_STR_ID_SHIFT) | |
48 | #define IPC_HEADER_LARGE_SHIFT 13 | |
49 | #define IPC_HEADER_LARGE(x) (((x) & 0x1) << IPC_HEADER_LARGE_SHIFT) | |
50 | #define IPC_HEADER_DATA_SHIFT 16 | |
51 | #define IPC_HEADER_DATA_MASK 0x3fff | |
52 | #define IPC_HEADER_DATA(x) (((x) & 0x3fff) << IPC_HEADER_DATA_SHIFT) | |
53 | ||
54 | /* mask for differentiating between notification and reply message */ | |
55 | #define IPC_NOTIFICATION (0x1 << 7) | |
56 | ||
57 | /* I2L Stream config/control msgs */ | |
58 | #define IPC_IA_ALLOC_STREAM 0x20 | |
59 | #define IPC_IA_FREE_STREAM 0x21 | |
60 | #define IPC_IA_PAUSE_STREAM 0x24 | |
61 | #define IPC_IA_RESUME_STREAM 0x25 | |
62 | #define IPC_IA_DROP_STREAM 0x26 | |
63 | #define IPC_IA_START_STREAM 0x30 | |
64 | ||
65 | /* notification messages */ | |
66 | #define IPC_IA_FW_INIT_CMPLT 0x81 | |
67 | #define IPC_SST_PERIOD_ELAPSED 0x97 | |
68 | ||
69 | /* IPC messages between host and ADSP */ | |
70 | struct sst_byt_address_info { | |
71 | u32 addr; | |
72 | u32 size; | |
73 | } __packed; | |
74 | ||
75 | struct sst_byt_str_type { | |
76 | u8 codec_type; | |
77 | u8 str_type; | |
78 | u8 operation; | |
79 | u8 protected_str; | |
80 | u8 time_slots; | |
81 | u8 reserved; | |
82 | u16 result; | |
83 | } __packed; | |
84 | ||
85 | struct sst_byt_pcm_params { | |
86 | u8 num_chan; | |
87 | u8 pcm_wd_sz; | |
88 | u8 use_offload_path; | |
89 | u8 reserved; | |
90 | u32 sfreq; | |
91 | u8 channel_map[8]; | |
92 | } __packed; | |
93 | ||
94 | struct sst_byt_frames_info { | |
95 | u16 num_entries; | |
96 | u16 rsrvd; | |
97 | u32 frag_size; | |
98 | struct sst_byt_address_info ring_buf_info[8]; | |
99 | } __packed; | |
100 | ||
101 | struct sst_byt_alloc_params { | |
102 | struct sst_byt_str_type str_type; | |
103 | struct sst_byt_pcm_params pcm_params; | |
104 | struct sst_byt_frames_info frame_info; | |
105 | } __packed; | |
106 | ||
107 | struct sst_byt_alloc_response { | |
108 | struct sst_byt_str_type str_type; | |
109 | u8 reserved[88]; | |
110 | } __packed; | |
111 | ||
112 | struct sst_byt_start_stream_params { | |
113 | u32 byte_offset; | |
114 | } __packed; | |
115 | ||
116 | struct sst_byt_tstamp { | |
117 | u64 ring_buffer_counter; | |
118 | u64 hardware_counter; | |
119 | u64 frames_decoded; | |
120 | u64 bytes_decoded; | |
121 | u64 bytes_copied; | |
122 | u32 sampling_frequency; | |
123 | u32 channel_peak[8]; | |
124 | } __packed; | |
125 | ||
126 | /* driver internal IPC message structure */ | |
127 | struct ipc_message { | |
128 | struct list_head list; | |
129 | u64 header; | |
130 | ||
131 | /* direction wrt host CPU */ | |
132 | char tx_data[SST_BYT_IPC_MAX_PAYLOAD_SIZE]; | |
133 | size_t tx_size; | |
134 | char rx_data[SST_BYT_IPC_MAX_PAYLOAD_SIZE]; | |
135 | size_t rx_size; | |
136 | ||
137 | wait_queue_head_t waitq; | |
138 | bool complete; | |
139 | bool wait; | |
140 | int errno; | |
141 | }; | |
142 | ||
143 | struct sst_byt_stream; | |
144 | struct sst_byt; | |
145 | ||
146 | /* stream infomation */ | |
147 | struct sst_byt_stream { | |
148 | struct list_head node; | |
149 | ||
150 | /* configuration */ | |
151 | struct sst_byt_alloc_params request; | |
152 | struct sst_byt_alloc_response reply; | |
153 | ||
154 | /* runtime info */ | |
155 | struct sst_byt *byt; | |
156 | int str_id; | |
157 | bool commited; | |
158 | bool running; | |
159 | ||
160 | /* driver callback */ | |
161 | u32 (*notify_position)(struct sst_byt_stream *stream, void *data); | |
162 | void *pdata; | |
163 | }; | |
164 | ||
165 | /* SST Baytrail IPC data */ | |
166 | struct sst_byt { | |
167 | struct device *dev; | |
168 | struct sst_dsp *dsp; | |
169 | ||
170 | /* stream */ | |
171 | struct list_head stream_list; | |
172 | ||
173 | /* boot */ | |
174 | wait_queue_head_t boot_wait; | |
175 | bool boot_complete; | |
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 | ||
302 | static int sst_byt_tx_wait_done(struct sst_byt *byt, struct ipc_message *msg, | |
303 | void *rx_data) | |
304 | { | |
305 | unsigned long flags; | |
306 | int ret; | |
307 | ||
308 | /* wait for DSP completion */ | |
309 | ret = wait_event_timeout(msg->waitq, msg->complete, | |
310 | msecs_to_jiffies(IPC_TIMEOUT_MSECS)); | |
311 | ||
312 | spin_lock_irqsave(&byt->dsp->spinlock, flags); | |
313 | if (ret == 0) { | |
314 | list_del(&msg->list); | |
315 | sst_byt_ipc_shim_dbg(byt, "message timeout"); | |
316 | ||
317 | ret = -ETIMEDOUT; | |
318 | } else { | |
319 | ||
320 | /* copy the data returned from DSP */ | |
321 | if (msg->rx_size) | |
322 | memcpy(rx_data, msg->rx_data, msg->rx_size); | |
323 | ret = msg->errno; | |
324 | } | |
325 | ||
326 | list_add_tail(&msg->list, &byt->empty_list); | |
327 | spin_unlock_irqrestore(&byt->dsp->spinlock, flags); | |
328 | return ret; | |
329 | } | |
330 | ||
331 | static int sst_byt_ipc_tx_message(struct sst_byt *byt, u64 header, | |
332 | void *tx_data, size_t tx_bytes, | |
333 | void *rx_data, size_t rx_bytes, int wait) | |
334 | { | |
335 | unsigned long flags; | |
336 | struct ipc_message *msg; | |
337 | ||
338 | spin_lock_irqsave(&byt->dsp->spinlock, flags); | |
339 | ||
340 | msg = sst_byt_msg_get_empty(byt); | |
341 | if (msg == NULL) { | |
342 | spin_unlock_irqrestore(&byt->dsp->spinlock, flags); | |
343 | return -EBUSY; | |
344 | } | |
345 | ||
346 | msg->header = header; | |
347 | msg->tx_size = tx_bytes; | |
348 | msg->rx_size = rx_bytes; | |
349 | msg->wait = wait; | |
350 | msg->errno = 0; | |
351 | msg->complete = false; | |
352 | ||
353 | if (tx_bytes) { | |
354 | /* msg content = lower 32-bit of the header + data */ | |
355 | *(u32 *)msg->tx_data = (u32)(header & (u32)-1); | |
356 | memcpy(msg->tx_data + sizeof(u32), tx_data, tx_bytes); | |
357 | msg->tx_size += sizeof(u32); | |
358 | } | |
359 | ||
360 | list_add_tail(&msg->list, &byt->tx_list); | |
361 | spin_unlock_irqrestore(&byt->dsp->spinlock, flags); | |
362 | ||
363 | queue_kthread_work(&byt->kworker, &byt->kwork); | |
364 | ||
365 | if (wait) | |
366 | return sst_byt_tx_wait_done(byt, msg, rx_data); | |
367 | else | |
368 | return 0; | |
369 | } | |
370 | ||
371 | static inline int sst_byt_ipc_tx_msg_wait(struct sst_byt *byt, u64 header, | |
372 | void *tx_data, size_t tx_bytes, | |
373 | void *rx_data, size_t rx_bytes) | |
374 | { | |
375 | return sst_byt_ipc_tx_message(byt, header, tx_data, tx_bytes, | |
376 | rx_data, rx_bytes, 1); | |
377 | } | |
378 | ||
379 | static inline int sst_byt_ipc_tx_msg_nowait(struct sst_byt *byt, u64 header, | |
380 | void *tx_data, size_t tx_bytes) | |
381 | { | |
382 | return sst_byt_ipc_tx_message(byt, header, tx_data, tx_bytes, | |
383 | NULL, 0, 0); | |
384 | } | |
385 | ||
386 | static struct ipc_message *sst_byt_reply_find_msg(struct sst_byt *byt, | |
387 | u64 header) | |
388 | { | |
389 | struct ipc_message *msg = NULL, *_msg; | |
390 | u64 mask; | |
391 | ||
392 | /* match reply to message sent based on msg and stream IDs */ | |
393 | mask = IPC_HEADER_MSG_ID_MASK | | |
394 | IPC_HEADER_STR_ID_MASK << IPC_HEADER_STR_ID_SHIFT; | |
395 | header &= mask; | |
396 | ||
397 | if (list_empty(&byt->rx_list)) { | |
398 | dev_err(byt->dev, | |
399 | "ipc: rx list is empty but received 0x%llx\n", header); | |
400 | goto out; | |
401 | } | |
402 | ||
403 | list_for_each_entry(_msg, &byt->rx_list, list) { | |
404 | if ((_msg->header & mask) == header) { | |
405 | msg = _msg; | |
406 | break; | |
407 | } | |
408 | } | |
409 | ||
410 | out: | |
411 | return msg; | |
412 | } | |
413 | ||
414 | static void sst_byt_stream_update(struct sst_byt *byt, struct ipc_message *msg) | |
415 | { | |
416 | struct sst_byt_stream *stream; | |
417 | u64 header = msg->header; | |
418 | u8 stream_id = sst_byt_header_str_id(header); | |
419 | u8 stream_msg = sst_byt_header_msg_id(header); | |
420 | ||
421 | stream = sst_byt_get_stream(byt, stream_id); | |
422 | if (stream == NULL) | |
423 | return; | |
424 | ||
425 | switch (stream_msg) { | |
426 | case IPC_IA_DROP_STREAM: | |
427 | case IPC_IA_PAUSE_STREAM: | |
428 | case IPC_IA_FREE_STREAM: | |
429 | stream->running = false; | |
430 | break; | |
431 | case IPC_IA_START_STREAM: | |
432 | case IPC_IA_RESUME_STREAM: | |
433 | stream->running = true; | |
434 | break; | |
435 | } | |
436 | } | |
437 | ||
438 | static int sst_byt_process_reply(struct sst_byt *byt, u64 header) | |
439 | { | |
440 | struct ipc_message *msg; | |
441 | ||
442 | msg = sst_byt_reply_find_msg(byt, header); | |
443 | if (msg == NULL) | |
444 | return 1; | |
445 | ||
446 | if (header & IPC_HEADER_LARGE(true)) { | |
447 | msg->rx_size = sst_byt_header_data(header); | |
448 | sst_dsp_inbox_read(byt->dsp, msg->rx_data, msg->rx_size); | |
449 | } | |
450 | ||
451 | /* update any stream states */ | |
452 | sst_byt_stream_update(byt, msg); | |
453 | ||
454 | list_del(&msg->list); | |
455 | /* wake up */ | |
456 | sst_byt_tx_msg_reply_complete(byt, msg); | |
457 | ||
458 | return 1; | |
459 | } | |
460 | ||
461 | static void sst_byt_fw_ready(struct sst_byt *byt, u64 header) | |
462 | { | |
463 | dev_dbg(byt->dev, "ipc: DSP is ready 0x%llX\n", header); | |
464 | ||
465 | byt->boot_complete = true; | |
466 | wake_up(&byt->boot_wait); | |
467 | } | |
468 | ||
469 | static int sst_byt_process_notification(struct sst_byt *byt, | |
470 | unsigned long *flags) | |
471 | { | |
472 | struct sst_dsp *sst = byt->dsp; | |
473 | struct sst_byt_stream *stream; | |
474 | u64 header; | |
475 | u8 msg_id, stream_id; | |
476 | int handled = 1; | |
477 | ||
478 | header = sst_dsp_shim_read64_unlocked(sst, SST_IPCD); | |
479 | msg_id = sst_byt_header_msg_id(header); | |
480 | ||
481 | switch (msg_id) { | |
482 | case IPC_SST_PERIOD_ELAPSED: | |
483 | stream_id = sst_byt_header_str_id(header); | |
484 | stream = sst_byt_get_stream(byt, stream_id); | |
485 | if (stream && stream->running && stream->notify_position) { | |
486 | spin_unlock_irqrestore(&sst->spinlock, *flags); | |
487 | stream->notify_position(stream, stream->pdata); | |
488 | spin_lock_irqsave(&sst->spinlock, *flags); | |
489 | } | |
490 | break; | |
491 | case IPC_IA_FW_INIT_CMPLT: | |
492 | sst_byt_fw_ready(byt, header); | |
493 | break; | |
494 | } | |
495 | ||
496 | return handled; | |
497 | } | |
498 | ||
499 | static irqreturn_t sst_byt_irq_thread(int irq, void *context) | |
500 | { | |
501 | struct sst_dsp *sst = (struct sst_dsp *) context; | |
502 | struct sst_byt *byt = sst_dsp_get_thread_context(sst); | |
503 | u64 header; | |
504 | unsigned long flags; | |
505 | ||
506 | spin_lock_irqsave(&sst->spinlock, flags); | |
507 | ||
508 | header = sst_dsp_shim_read64_unlocked(sst, SST_IPCD); | |
509 | if (header & SST_BYT_IPCD_BUSY) { | |
510 | if (header & IPC_NOTIFICATION) { | |
511 | /* message from ADSP */ | |
512 | sst_byt_process_notification(byt, &flags); | |
513 | } else { | |
514 | /* reply from ADSP */ | |
515 | sst_byt_process_reply(byt, header); | |
516 | } | |
517 | /* | |
518 | * clear IPCD BUSY bit and set DONE bit. Tell DSP we have | |
519 | * processed the message and can accept new. Clear data part | |
520 | * of the header | |
521 | */ | |
522 | sst_dsp_shim_update_bits64_unlocked(sst, SST_IPCD, | |
523 | SST_BYT_IPCD_DONE | SST_BYT_IPCD_BUSY | | |
524 | IPC_HEADER_DATA(IPC_HEADER_DATA_MASK), | |
525 | SST_BYT_IPCD_DONE); | |
526 | /* unmask message request interrupts */ | |
527 | sst_dsp_shim_update_bits64_unlocked(sst, SST_IMRX, | |
528 | SST_BYT_IMRX_REQUEST, 0); | |
529 | } | |
530 | ||
531 | spin_unlock_irqrestore(&sst->spinlock, flags); | |
532 | ||
533 | /* continue to send any remaining messages... */ | |
534 | queue_kthread_work(&byt->kworker, &byt->kwork); | |
535 | ||
536 | return IRQ_HANDLED; | |
537 | } | |
538 | ||
539 | /* stream API */ | |
540 | struct sst_byt_stream *sst_byt_stream_new(struct sst_byt *byt, int id, | |
541 | u32 (*notify_position)(struct sst_byt_stream *stream, void *data), | |
542 | void *data) | |
543 | { | |
544 | struct sst_byt_stream *stream; | |
545 | ||
546 | stream = kzalloc(sizeof(*stream), GFP_KERNEL); | |
547 | if (stream == NULL) | |
548 | return NULL; | |
549 | ||
550 | list_add(&stream->node, &byt->stream_list); | |
551 | stream->notify_position = notify_position; | |
552 | stream->pdata = data; | |
553 | stream->byt = byt; | |
554 | stream->str_id = id; | |
555 | ||
556 | return stream; | |
557 | } | |
558 | ||
559 | int sst_byt_stream_set_bits(struct sst_byt *byt, struct sst_byt_stream *stream, | |
560 | int bits) | |
561 | { | |
562 | stream->request.pcm_params.pcm_wd_sz = bits; | |
563 | return 0; | |
564 | } | |
565 | ||
566 | int sst_byt_stream_set_channels(struct sst_byt *byt, | |
567 | struct sst_byt_stream *stream, u8 channels) | |
568 | { | |
569 | stream->request.pcm_params.num_chan = channels; | |
570 | return 0; | |
571 | } | |
572 | ||
573 | int sst_byt_stream_set_rate(struct sst_byt *byt, struct sst_byt_stream *stream, | |
574 | unsigned int rate) | |
575 | { | |
576 | stream->request.pcm_params.sfreq = rate; | |
577 | return 0; | |
578 | } | |
579 | ||
580 | /* stream sonfiguration */ | |
581 | int sst_byt_stream_type(struct sst_byt *byt, struct sst_byt_stream *stream, | |
582 | int codec_type, int stream_type, int operation) | |
583 | { | |
584 | stream->request.str_type.codec_type = codec_type; | |
585 | stream->request.str_type.str_type = stream_type; | |
586 | stream->request.str_type.operation = operation; | |
587 | stream->request.str_type.time_slots = 0xc; | |
588 | ||
589 | return 0; | |
590 | } | |
591 | ||
592 | int sst_byt_stream_buffer(struct sst_byt *byt, struct sst_byt_stream *stream, | |
593 | uint32_t buffer_addr, uint32_t buffer_size) | |
594 | { | |
595 | stream->request.frame_info.num_entries = 1; | |
596 | stream->request.frame_info.ring_buf_info[0].addr = buffer_addr; | |
597 | stream->request.frame_info.ring_buf_info[0].size = buffer_size; | |
598 | /* calculate bytes per 4 ms fragment */ | |
599 | stream->request.frame_info.frag_size = | |
600 | stream->request.pcm_params.sfreq * | |
601 | stream->request.pcm_params.num_chan * | |
602 | stream->request.pcm_params.pcm_wd_sz / 8 * | |
603 | 4 / 1000; | |
604 | return 0; | |
605 | } | |
606 | ||
607 | int sst_byt_stream_commit(struct sst_byt *byt, struct sst_byt_stream *stream) | |
608 | { | |
609 | struct sst_byt_alloc_params *str_req = &stream->request; | |
610 | struct sst_byt_alloc_response *reply = &stream->reply; | |
611 | u64 header; | |
612 | int ret; | |
613 | ||
614 | header = sst_byt_header(IPC_IA_ALLOC_STREAM, | |
615 | sizeof(*str_req) + sizeof(u32), | |
616 | true, stream->str_id); | |
617 | ret = sst_byt_ipc_tx_msg_wait(byt, header, str_req, sizeof(*str_req), | |
618 | reply, sizeof(*reply)); | |
619 | if (ret < 0) { | |
620 | dev_err(byt->dev, "ipc: error stream commit failed\n"); | |
621 | return ret; | |
622 | } | |
623 | ||
624 | stream->commited = true; | |
625 | ||
626 | return 0; | |
627 | } | |
628 | ||
629 | int sst_byt_stream_free(struct sst_byt *byt, struct sst_byt_stream *stream) | |
630 | { | |
631 | u64 header; | |
632 | int ret = 0; | |
633 | ||
634 | if (!stream->commited) | |
635 | goto out; | |
636 | ||
637 | header = sst_byt_header(IPC_IA_FREE_STREAM, 0, false, stream->str_id); | |
638 | ret = sst_byt_ipc_tx_msg_wait(byt, header, NULL, 0, NULL, 0); | |
639 | if (ret < 0) { | |
640 | dev_err(byt->dev, "ipc: free stream %d failed\n", | |
641 | stream->str_id); | |
642 | return -EAGAIN; | |
643 | } | |
644 | ||
645 | stream->commited = false; | |
646 | out: | |
647 | list_del(&stream->node); | |
648 | kfree(stream); | |
649 | ||
650 | return ret; | |
651 | } | |
652 | ||
653 | static int sst_byt_stream_operations(struct sst_byt *byt, int type, | |
654 | int stream_id, int wait) | |
655 | { | |
a4b12990 | 656 | u64 header; |
a4b12990 | 657 | |
65ee9e8f | 658 | header = sst_byt_header(type, 0, false, stream_id); |
a4b12990 | 659 | if (wait) |
65ee9e8f | 660 | return sst_byt_ipc_tx_msg_wait(byt, header, NULL, 0, NULL, 0); |
a4b12990 | 661 | else |
65ee9e8f | 662 | return sst_byt_ipc_tx_msg_nowait(byt, header, NULL, 0); |
a4b12990 MB |
663 | } |
664 | ||
665 | /* stream ALSA trigger operations */ | |
666 | int sst_byt_stream_start(struct sst_byt *byt, struct sst_byt_stream *stream) | |
667 | { | |
65ee9e8f JN |
668 | struct sst_byt_start_stream_params start_stream; |
669 | void *tx_msg; | |
670 | size_t size; | |
671 | u64 header; | |
a4b12990 MB |
672 | int ret; |
673 | ||
65ee9e8f JN |
674 | start_stream.byte_offset = 0; |
675 | header = sst_byt_header(IPC_IA_START_STREAM, | |
676 | sizeof(start_stream) + sizeof(u32), | |
677 | true, stream->str_id); | |
678 | tx_msg = &start_stream; | |
679 | size = sizeof(start_stream); | |
680 | ||
681 | ret = sst_byt_ipc_tx_msg_nowait(byt, header, tx_msg, size); | |
a4b12990 MB |
682 | if (ret < 0) |
683 | dev_err(byt->dev, "ipc: error failed to start stream %d\n", | |
684 | stream->str_id); | |
685 | ||
686 | return ret; | |
687 | } | |
688 | ||
689 | int sst_byt_stream_stop(struct sst_byt *byt, struct sst_byt_stream *stream) | |
690 | { | |
691 | int ret; | |
692 | ||
693 | /* don't stop streams that are not commited */ | |
694 | if (!stream->commited) | |
695 | return 0; | |
696 | ||
697 | ret = sst_byt_stream_operations(byt, IPC_IA_DROP_STREAM, | |
698 | stream->str_id, 0); | |
699 | if (ret < 0) | |
700 | dev_err(byt->dev, "ipc: error failed to stop stream %d\n", | |
701 | stream->str_id); | |
702 | return ret; | |
703 | } | |
704 | ||
705 | int sst_byt_stream_pause(struct sst_byt *byt, struct sst_byt_stream *stream) | |
706 | { | |
707 | int ret; | |
708 | ||
709 | ret = sst_byt_stream_operations(byt, IPC_IA_PAUSE_STREAM, | |
710 | stream->str_id, 0); | |
711 | if (ret < 0) | |
712 | dev_err(byt->dev, "ipc: error failed to pause stream %d\n", | |
713 | stream->str_id); | |
714 | ||
715 | return ret; | |
716 | } | |
717 | ||
718 | int sst_byt_stream_resume(struct sst_byt *byt, struct sst_byt_stream *stream) | |
719 | { | |
720 | int ret; | |
721 | ||
722 | ret = sst_byt_stream_operations(byt, IPC_IA_RESUME_STREAM, | |
723 | stream->str_id, 0); | |
724 | if (ret < 0) | |
725 | dev_err(byt->dev, "ipc: error failed to resume stream %d\n", | |
726 | stream->str_id); | |
727 | ||
728 | return ret; | |
729 | } | |
730 | ||
731 | int sst_byt_get_dsp_position(struct sst_byt *byt, | |
732 | struct sst_byt_stream *stream, int buffer_size) | |
733 | { | |
734 | struct sst_dsp *sst = byt->dsp; | |
735 | struct sst_byt_tstamp fw_tstamp; | |
736 | u8 str_id = stream->str_id; | |
737 | u32 tstamp_offset; | |
738 | ||
739 | tstamp_offset = SST_BYT_TIMESTAMP_OFFSET + str_id * sizeof(fw_tstamp); | |
740 | memcpy_fromio(&fw_tstamp, | |
741 | sst->addr.lpe + tstamp_offset, sizeof(fw_tstamp)); | |
742 | ||
743 | return do_div(fw_tstamp.ring_buffer_counter, buffer_size); | |
744 | } | |
745 | ||
746 | static int msg_empty_list_init(struct sst_byt *byt) | |
747 | { | |
748 | struct ipc_message *msg; | |
749 | int i; | |
750 | ||
751 | byt->msg = kzalloc(sizeof(*msg) * IPC_EMPTY_LIST_SIZE, GFP_KERNEL); | |
752 | if (byt->msg == NULL) | |
753 | return -ENOMEM; | |
754 | ||
755 | for (i = 0; i < IPC_EMPTY_LIST_SIZE; i++) { | |
756 | init_waitqueue_head(&byt->msg[i].waitq); | |
757 | list_add(&byt->msg[i].list, &byt->empty_list); | |
758 | } | |
759 | ||
760 | return 0; | |
761 | } | |
762 | ||
763 | struct sst_dsp *sst_byt_get_dsp(struct sst_byt *byt) | |
764 | { | |
765 | return byt->dsp; | |
766 | } | |
767 | ||
768 | static struct sst_dsp_device byt_dev = { | |
769 | .thread = sst_byt_irq_thread, | |
770 | .ops = &sst_byt_ops, | |
771 | }; | |
772 | ||
773 | int sst_byt_dsp_init(struct device *dev, struct sst_pdata *pdata) | |
774 | { | |
775 | struct sst_byt *byt; | |
776 | struct sst_fw *byt_sst_fw; | |
777 | int err; | |
778 | ||
779 | dev_dbg(dev, "initialising Byt DSP IPC\n"); | |
780 | ||
781 | byt = devm_kzalloc(dev, sizeof(*byt), GFP_KERNEL); | |
782 | if (byt == NULL) | |
783 | return -ENOMEM; | |
784 | ||
785 | byt->dev = dev; | |
786 | INIT_LIST_HEAD(&byt->stream_list); | |
787 | INIT_LIST_HEAD(&byt->tx_list); | |
788 | INIT_LIST_HEAD(&byt->rx_list); | |
789 | INIT_LIST_HEAD(&byt->empty_list); | |
790 | init_waitqueue_head(&byt->boot_wait); | |
791 | init_waitqueue_head(&byt->wait_txq); | |
792 | ||
793 | err = msg_empty_list_init(byt); | |
794 | if (err < 0) | |
795 | return -ENOMEM; | |
796 | ||
797 | /* start the IPC message thread */ | |
798 | init_kthread_worker(&byt->kworker); | |
799 | byt->tx_thread = kthread_run(kthread_worker_fn, | |
800 | &byt->kworker, | |
801 | dev_name(byt->dev)); | |
802 | if (IS_ERR(byt->tx_thread)) { | |
803 | err = PTR_ERR(byt->tx_thread); | |
804 | dev_err(byt->dev, "error failed to create message TX task\n"); | |
805 | goto err_free_msg; | |
806 | } | |
807 | init_kthread_work(&byt->kwork, sst_byt_ipc_tx_msgs); | |
808 | ||
809 | byt_dev.thread_context = byt; | |
810 | ||
811 | /* init SST shim */ | |
812 | byt->dsp = sst_dsp_new(dev, &byt_dev, pdata); | |
813 | if (byt->dsp == NULL) { | |
814 | err = -ENODEV; | |
815 | goto err_free_msg; | |
816 | } | |
817 | ||
818 | /* keep the DSP in reset state for base FW loading */ | |
819 | sst_dsp_reset(byt->dsp); | |
820 | ||
821 | byt_sst_fw = sst_fw_new(byt->dsp, pdata->fw, byt); | |
822 | if (byt_sst_fw == NULL) { | |
823 | err = -ENODEV; | |
824 | dev_err(dev, "error: failed to load firmware\n"); | |
825 | goto fw_err; | |
826 | } | |
827 | ||
828 | /* wait for DSP boot completion */ | |
829 | sst_dsp_boot(byt->dsp); | |
830 | err = wait_event_timeout(byt->boot_wait, byt->boot_complete, | |
831 | msecs_to_jiffies(IPC_BOOT_MSECS)); | |
832 | if (err == 0) { | |
833 | err = -EIO; | |
834 | dev_err(byt->dev, "ipc: error DSP boot timeout\n"); | |
835 | goto boot_err; | |
836 | } | |
837 | ||
838 | pdata->dsp = byt; | |
839 | ||
840 | return 0; | |
841 | ||
842 | boot_err: | |
843 | sst_dsp_reset(byt->dsp); | |
844 | sst_fw_free(byt_sst_fw); | |
845 | fw_err: | |
846 | sst_dsp_free(byt->dsp); | |
847 | err_free_msg: | |
848 | kfree(byt->msg); | |
849 | ||
850 | return err; | |
851 | } | |
852 | EXPORT_SYMBOL_GPL(sst_byt_dsp_init); | |
853 | ||
854 | void sst_byt_dsp_free(struct device *dev, struct sst_pdata *pdata) | |
855 | { | |
856 | struct sst_byt *byt = pdata->dsp; | |
857 | ||
858 | sst_dsp_reset(byt->dsp); | |
859 | sst_fw_free_all(byt->dsp); | |
860 | sst_dsp_free(byt->dsp); | |
861 | kfree(byt->msg); | |
862 | } | |
863 | EXPORT_SYMBOL_GPL(sst_byt_dsp_free); |