~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/soc/intel/sst-baytrail-ipc.c

Version: ~ [ linux-5.8 ] ~ [ linux-5.7.14 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.57 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.138 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.193 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.232 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.232 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp