Linux 6.7-rc7
[linux-modified.git] / sound / soc / sof / core.c
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2 //
3 // This file is provided under a dual BSD/GPLv2 license.  When using or
4 // redistributing this file, you may do so under either license.
5 //
6 // Copyright(c) 2018 Intel Corporation. All rights reserved.
7 //
8 // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9 //
10
11 #include <linux/firmware.h>
12 #include <linux/module.h>
13 #include <sound/soc.h>
14 #include <sound/sof.h>
15 #include "sof-priv.h"
16 #include "ops.h"
17
18 #define CREATE_TRACE_POINTS
19 #include <trace/events/sof.h>
20
21 /* see SOF_DBG_ flags */
22 static int sof_core_debug =  IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_FIRMWARE_TRACE);
23 module_param_named(sof_debug, sof_core_debug, int, 0444);
24 MODULE_PARM_DESC(sof_debug, "SOF core debug options (0x0 all off)");
25
26 /* SOF defaults if not provided by the platform in ms */
27 #define TIMEOUT_DEFAULT_IPC_MS  500
28 #define TIMEOUT_DEFAULT_BOOT_MS 2000
29
30 /**
31  * sof_debug_check_flag - check if a given flag(s) is set in sof_core_debug
32  * @mask: Flag or combination of flags to check
33  *
34  * Returns true if all bits set in mask is also set in sof_core_debug, otherwise
35  * false
36  */
37 bool sof_debug_check_flag(int mask)
38 {
39         if ((sof_core_debug & mask) == mask)
40                 return true;
41
42         return false;
43 }
44 EXPORT_SYMBOL(sof_debug_check_flag);
45
46 /*
47  * FW Panic/fault handling.
48  */
49
50 struct sof_panic_msg {
51         u32 id;
52         const char *msg;
53 };
54
55 /* standard FW panic types */
56 static const struct sof_panic_msg panic_msg[] = {
57         {SOF_IPC_PANIC_MEM, "out of memory"},
58         {SOF_IPC_PANIC_WORK, "work subsystem init failed"},
59         {SOF_IPC_PANIC_IPC, "IPC subsystem init failed"},
60         {SOF_IPC_PANIC_ARCH, "arch init failed"},
61         {SOF_IPC_PANIC_PLATFORM, "platform init failed"},
62         {SOF_IPC_PANIC_TASK, "scheduler init failed"},
63         {SOF_IPC_PANIC_EXCEPTION, "runtime exception"},
64         {SOF_IPC_PANIC_DEADLOCK, "deadlock"},
65         {SOF_IPC_PANIC_STACK, "stack overflow"},
66         {SOF_IPC_PANIC_IDLE, "can't enter idle"},
67         {SOF_IPC_PANIC_WFI, "invalid wait state"},
68         {SOF_IPC_PANIC_ASSERT, "assertion failed"},
69 };
70
71 /**
72  * sof_print_oops_and_stack - Handle the printing of DSP oops and stack trace
73  * @sdev: Pointer to the device's sdev
74  * @level: prink log level to use for the printing
75  * @panic_code: the panic code
76  * @tracep_code: tracepoint code
77  * @oops: Pointer to DSP specific oops data
78  * @panic_info: Pointer to the received panic information message
79  * @stack: Pointer to the call stack data
80  * @stack_words: Number of words in the stack data
81  *
82  * helper to be called from .dbg_dump callbacks. No error code is
83  * provided, it's left as an exercise for the caller of .dbg_dump
84  * (typically IPC or loader)
85  */
86 void sof_print_oops_and_stack(struct snd_sof_dev *sdev, const char *level,
87                               u32 panic_code, u32 tracep_code, void *oops,
88                               struct sof_ipc_panic_info *panic_info,
89                               void *stack, size_t stack_words)
90 {
91         u32 code;
92         int i;
93
94         /* is firmware dead ? */
95         if ((panic_code & SOF_IPC_PANIC_MAGIC_MASK) != SOF_IPC_PANIC_MAGIC) {
96                 dev_printk(level, sdev->dev, "unexpected fault %#010x trace %#010x\n",
97                            panic_code, tracep_code);
98                 return; /* no fault ? */
99         }
100
101         code = panic_code & (SOF_IPC_PANIC_MAGIC_MASK | SOF_IPC_PANIC_CODE_MASK);
102
103         for (i = 0; i < ARRAY_SIZE(panic_msg); i++) {
104                 if (panic_msg[i].id == code) {
105                         dev_printk(level, sdev->dev, "reason: %s (%#x)\n",
106                                    panic_msg[i].msg, code & SOF_IPC_PANIC_CODE_MASK);
107                         dev_printk(level, sdev->dev, "trace point: %#010x\n", tracep_code);
108                         goto out;
109                 }
110         }
111
112         /* unknown error */
113         dev_printk(level, sdev->dev, "unknown panic code: %#x\n",
114                    code & SOF_IPC_PANIC_CODE_MASK);
115         dev_printk(level, sdev->dev, "trace point: %#010x\n", tracep_code);
116
117 out:
118         dev_printk(level, sdev->dev, "panic at %s:%d\n", panic_info->filename,
119                    panic_info->linenum);
120         sof_oops(sdev, level, oops);
121         sof_stack(sdev, level, oops, stack, stack_words);
122 }
123 EXPORT_SYMBOL(sof_print_oops_and_stack);
124
125 /* Helper to manage DSP state */
126 void sof_set_fw_state(struct snd_sof_dev *sdev, enum sof_fw_state new_state)
127 {
128         if (sdev->fw_state == new_state)
129                 return;
130
131         dev_dbg(sdev->dev, "fw_state change: %d -> %d\n", sdev->fw_state, new_state);
132         sdev->fw_state = new_state;
133
134         switch (new_state) {
135         case SOF_FW_BOOT_NOT_STARTED:
136         case SOF_FW_BOOT_COMPLETE:
137         case SOF_FW_CRASHED:
138                 sof_client_fw_state_dispatcher(sdev);
139                 fallthrough;
140         default:
141                 break;
142         }
143 }
144 EXPORT_SYMBOL(sof_set_fw_state);
145
146 /*
147  *                      FW Boot State Transition Diagram
148  *
149  *    +----------------------------------------------------------------------+
150  *    |                                                                      |
151  * ------------------        ------------------                              |
152  * |                |        |                |                              |
153  * |   BOOT_FAILED  |<-------|  READY_FAILED  |                              |
154  * |                |<--+    |                |    ------------------        |
155  * ------------------   |    ------------------    |                |        |
156  *      ^               |           ^              |    CRASHED     |---+    |
157  *      |               |           |              |                |   |    |
158  * (FW Boot Timeout)    |       (FW_READY FAIL)    ------------------   |    |
159  *      |               |           |                ^                  |    |
160  *      |               |           |                |(DSP Panic)       |    |
161  * ------------------   |           |              ------------------   |    |
162  * |                |   |           |              |                |   |    |
163  * |   IN_PROGRESS  |---------------+------------->|    COMPLETE    |   |    |
164  * |                | (FW Boot OK)   (FW_READY OK) |                |   |    |
165  * ------------------   |                          ------------------   |    |
166  *      ^               |                               |               |    |
167  *      |               |                               |               |    |
168  * (FW Loading OK)      |                       (System Suspend/Runtime Suspend)
169  *      |               |                               |               |    |
170  *      |       (FW Loading Fail)                       |               |    |
171  * ------------------   |       ------------------      |               |    |
172  * |                |   |       |                |<-----+               |    |
173  * |   PREPARE      |---+       |   NOT_STARTED  |<---------------------+    |
174  * |                |           |                |<--------------------------+
175  * ------------------           ------------------
176  *    |     ^                       |      ^
177  *    |     |                       |      |
178  *    |     +-----------------------+      |
179  *    |         (DSP Probe OK)             |
180  *    |                                    |
181  *    |                                    |
182  *    +------------------------------------+
183  *      (System Suspend/Runtime Suspend)
184  */
185
186 static int sof_probe_continue(struct snd_sof_dev *sdev)
187 {
188         struct snd_sof_pdata *plat_data = sdev->pdata;
189         int ret;
190
191         /* probe the DSP hardware */
192         ret = snd_sof_probe(sdev);
193         if (ret < 0) {
194                 dev_err(sdev->dev, "error: failed to probe DSP %d\n", ret);
195                 goto probe_err;
196         }
197
198         sof_set_fw_state(sdev, SOF_FW_BOOT_PREPARE);
199
200         /* check machine info */
201         ret = sof_machine_check(sdev);
202         if (ret < 0) {
203                 dev_err(sdev->dev, "error: failed to get machine info %d\n",
204                         ret);
205                 goto dsp_err;
206         }
207
208         /* set up platform component driver */
209         snd_sof_new_platform_drv(sdev);
210
211         if (sdev->dspless_mode_selected) {
212                 sof_set_fw_state(sdev, SOF_DSPLESS_MODE);
213                 goto skip_dsp_init;
214         }
215
216         /* register any debug/trace capabilities */
217         ret = snd_sof_dbg_init(sdev);
218         if (ret < 0) {
219                 /*
220                  * debugfs issues are suppressed in snd_sof_dbg_init() since
221                  * we cannot rely on debugfs
222                  * here we trap errors due to memory allocation only.
223                  */
224                 dev_err(sdev->dev, "error: failed to init DSP trace/debug %d\n",
225                         ret);
226                 goto dbg_err;
227         }
228
229         /* init the IPC */
230         sdev->ipc = snd_sof_ipc_init(sdev);
231         if (!sdev->ipc) {
232                 ret = -ENOMEM;
233                 dev_err(sdev->dev, "error: failed to init DSP IPC %d\n", ret);
234                 goto ipc_err;
235         }
236
237         /* load the firmware */
238         ret = snd_sof_load_firmware(sdev);
239         if (ret < 0) {
240                 dev_err(sdev->dev, "error: failed to load DSP firmware %d\n",
241                         ret);
242                 sof_set_fw_state(sdev, SOF_FW_BOOT_FAILED);
243                 goto fw_load_err;
244         }
245
246         sof_set_fw_state(sdev, SOF_FW_BOOT_IN_PROGRESS);
247
248         /*
249          * Boot the firmware. The FW boot status will be modified
250          * in snd_sof_run_firmware() depending on the outcome.
251          */
252         ret = snd_sof_run_firmware(sdev);
253         if (ret < 0) {
254                 dev_err(sdev->dev, "error: failed to boot DSP firmware %d\n",
255                         ret);
256                 sof_set_fw_state(sdev, SOF_FW_BOOT_FAILED);
257                 goto fw_run_err;
258         }
259
260         if (sof_debug_check_flag(SOF_DBG_ENABLE_TRACE)) {
261                 sdev->fw_trace_is_supported = true;
262
263                 /* init firmware tracing */
264                 ret = sof_fw_trace_init(sdev);
265                 if (ret < 0) {
266                         /* non fatal */
267                         dev_warn(sdev->dev, "failed to initialize firmware tracing %d\n",
268                                  ret);
269                 }
270         } else {
271                 dev_dbg(sdev->dev, "SOF firmware trace disabled\n");
272         }
273
274 skip_dsp_init:
275         /* hereafter all FW boot flows are for PM reasons */
276         sdev->first_boot = false;
277
278         /* now register audio DSP platform driver and dai */
279         ret = devm_snd_soc_register_component(sdev->dev, &sdev->plat_drv,
280                                               sof_ops(sdev)->drv,
281                                               sof_ops(sdev)->num_drv);
282         if (ret < 0) {
283                 dev_err(sdev->dev,
284                         "error: failed to register DSP DAI driver %d\n", ret);
285                 goto fw_trace_err;
286         }
287
288         ret = snd_sof_machine_register(sdev, plat_data);
289         if (ret < 0) {
290                 dev_err(sdev->dev,
291                         "error: failed to register machine driver %d\n", ret);
292                 goto fw_trace_err;
293         }
294
295         ret = sof_register_clients(sdev);
296         if (ret < 0) {
297                 dev_err(sdev->dev, "failed to register clients %d\n", ret);
298                 goto sof_machine_err;
299         }
300
301         /*
302          * Some platforms in SOF, ex: BYT, may not have their platform PM
303          * callbacks set. Increment the usage count so as to
304          * prevent the device from entering runtime suspend.
305          */
306         if (!sof_ops(sdev)->runtime_suspend || !sof_ops(sdev)->runtime_resume)
307                 pm_runtime_get_noresume(sdev->dev);
308
309         if (plat_data->sof_probe_complete)
310                 plat_data->sof_probe_complete(sdev->dev);
311
312         sdev->probe_completed = true;
313
314         return 0;
315
316 sof_machine_err:
317         snd_sof_machine_unregister(sdev, plat_data);
318 fw_trace_err:
319         sof_fw_trace_free(sdev);
320 fw_run_err:
321         snd_sof_fw_unload(sdev);
322 fw_load_err:
323         snd_sof_ipc_free(sdev);
324 ipc_err:
325 dbg_err:
326         snd_sof_free_debug(sdev);
327 dsp_err:
328         snd_sof_remove(sdev);
329 probe_err:
330         snd_sof_remove_late(sdev);
331         sof_ops_free(sdev);
332
333         /* all resources freed, update state to match */
334         sof_set_fw_state(sdev, SOF_FW_BOOT_NOT_STARTED);
335         sdev->first_boot = true;
336
337         return ret;
338 }
339
340 static void sof_probe_work(struct work_struct *work)
341 {
342         struct snd_sof_dev *sdev =
343                 container_of(work, struct snd_sof_dev, probe_work);
344         int ret;
345
346         ret = sof_probe_continue(sdev);
347         if (ret < 0) {
348                 /* errors cannot be propagated, log */
349                 dev_err(sdev->dev, "error: %s failed err: %d\n", __func__, ret);
350         }
351 }
352
353 int snd_sof_device_probe(struct device *dev, struct snd_sof_pdata *plat_data)
354 {
355         struct snd_sof_dev *sdev;
356         int ret;
357
358         sdev = devm_kzalloc(dev, sizeof(*sdev), GFP_KERNEL);
359         if (!sdev)
360                 return -ENOMEM;
361
362         /* initialize sof device */
363         sdev->dev = dev;
364
365         /* initialize default DSP power state */
366         sdev->dsp_power_state.state = SOF_DSP_PM_D0;
367
368         sdev->pdata = plat_data;
369         sdev->first_boot = true;
370         dev_set_drvdata(dev, sdev);
371
372         if (sof_core_debug)
373                 dev_info(dev, "sof_debug value: %#x\n", sof_core_debug);
374
375         if (sof_debug_check_flag(SOF_DBG_DSPLESS_MODE)) {
376                 if (plat_data->desc->dspless_mode_supported) {
377                         dev_info(dev, "Switching to DSPless mode\n");
378                         sdev->dspless_mode_selected = true;
379                 } else {
380                         dev_info(dev, "DSPless mode is not supported by the platform\n");
381                 }
382         }
383
384         /* check IPC support */
385         if (!(BIT(plat_data->ipc_type) & plat_data->desc->ipc_supported_mask)) {
386                 dev_err(dev, "ipc_type %d is not supported on this platform, mask is %#x\n",
387                         plat_data->ipc_type, plat_data->desc->ipc_supported_mask);
388                 return -EINVAL;
389         }
390
391         /* init ops, if necessary */
392         ret = sof_ops_init(sdev);
393         if (ret < 0)
394                 return ret;
395
396         /* check all mandatory ops */
397         if (!sof_ops(sdev) || !sof_ops(sdev)->probe) {
398                 sof_ops_free(sdev);
399                 dev_err(dev, "missing mandatory ops\n");
400                 return -EINVAL;
401         }
402
403         if (!sdev->dspless_mode_selected &&
404             (!sof_ops(sdev)->run || !sof_ops(sdev)->block_read ||
405              !sof_ops(sdev)->block_write || !sof_ops(sdev)->send_msg ||
406              !sof_ops(sdev)->load_firmware || !sof_ops(sdev)->ipc_msg_data)) {
407                 sof_ops_free(sdev);
408                 dev_err(dev, "missing mandatory DSP ops\n");
409                 return -EINVAL;
410         }
411
412         INIT_LIST_HEAD(&sdev->pcm_list);
413         INIT_LIST_HEAD(&sdev->kcontrol_list);
414         INIT_LIST_HEAD(&sdev->widget_list);
415         INIT_LIST_HEAD(&sdev->pipeline_list);
416         INIT_LIST_HEAD(&sdev->dai_list);
417         INIT_LIST_HEAD(&sdev->dai_link_list);
418         INIT_LIST_HEAD(&sdev->route_list);
419         INIT_LIST_HEAD(&sdev->ipc_client_list);
420         INIT_LIST_HEAD(&sdev->ipc_rx_handler_list);
421         INIT_LIST_HEAD(&sdev->fw_state_handler_list);
422         spin_lock_init(&sdev->ipc_lock);
423         spin_lock_init(&sdev->hw_lock);
424         mutex_init(&sdev->power_state_access);
425         mutex_init(&sdev->ipc_client_mutex);
426         mutex_init(&sdev->client_event_handler_mutex);
427
428         /* set default timeouts if none provided */
429         if (plat_data->desc->ipc_timeout == 0)
430                 sdev->ipc_timeout = TIMEOUT_DEFAULT_IPC_MS;
431         else
432                 sdev->ipc_timeout = plat_data->desc->ipc_timeout;
433         if (plat_data->desc->boot_timeout == 0)
434                 sdev->boot_timeout = TIMEOUT_DEFAULT_BOOT_MS;
435         else
436                 sdev->boot_timeout = plat_data->desc->boot_timeout;
437
438         sof_set_fw_state(sdev, SOF_FW_BOOT_NOT_STARTED);
439
440         /*
441          * first pass of probe which isn't allowed to run in a work-queue,
442          * typically to rely on -EPROBE_DEFER dependencies
443          */
444         ret = snd_sof_probe_early(sdev);
445         if (ret < 0)
446                 return ret;
447
448         if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE)) {
449                 INIT_WORK(&sdev->probe_work, sof_probe_work);
450                 schedule_work(&sdev->probe_work);
451                 return 0;
452         }
453
454         return sof_probe_continue(sdev);
455 }
456 EXPORT_SYMBOL(snd_sof_device_probe);
457
458 bool snd_sof_device_probe_completed(struct device *dev)
459 {
460         struct snd_sof_dev *sdev = dev_get_drvdata(dev);
461
462         return sdev->probe_completed;
463 }
464 EXPORT_SYMBOL(snd_sof_device_probe_completed);
465
466 int snd_sof_device_remove(struct device *dev)
467 {
468         struct snd_sof_dev *sdev = dev_get_drvdata(dev);
469         struct snd_sof_pdata *pdata = sdev->pdata;
470         int ret;
471         bool aborted = false;
472
473         if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE))
474                 aborted = cancel_work_sync(&sdev->probe_work);
475
476         /*
477          * Unregister any registered client device first before IPC and debugfs
478          * to allow client drivers to be removed cleanly
479          */
480         sof_unregister_clients(sdev);
481
482         /*
483          * Unregister machine driver. This will unbind the snd_card which
484          * will remove the component driver and unload the topology
485          * before freeing the snd_card.
486          */
487         snd_sof_machine_unregister(sdev, pdata);
488
489         if (sdev->fw_state > SOF_FW_BOOT_NOT_STARTED) {
490                 sof_fw_trace_free(sdev);
491                 ret = snd_sof_dsp_power_down_notify(sdev);
492                 if (ret < 0)
493                         dev_warn(dev, "error: %d failed to prepare DSP for device removal",
494                                  ret);
495
496                 snd_sof_ipc_free(sdev);
497                 snd_sof_free_debug(sdev);
498                 snd_sof_remove(sdev);
499                 snd_sof_remove_late(sdev);
500                 sof_ops_free(sdev);
501         } else if (aborted) {
502                 /* probe_work never ran */
503                 snd_sof_remove_late(sdev);
504                 sof_ops_free(sdev);
505         }
506
507         /* release firmware */
508         snd_sof_fw_unload(sdev);
509
510         return 0;
511 }
512 EXPORT_SYMBOL(snd_sof_device_remove);
513
514 int snd_sof_device_shutdown(struct device *dev)
515 {
516         struct snd_sof_dev *sdev = dev_get_drvdata(dev);
517
518         if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE))
519                 cancel_work_sync(&sdev->probe_work);
520
521         if (sdev->fw_state == SOF_FW_BOOT_COMPLETE) {
522                 sof_fw_trace_free(sdev);
523                 return snd_sof_shutdown(sdev);
524         }
525
526         return 0;
527 }
528 EXPORT_SYMBOL(snd_sof_device_shutdown);
529
530 MODULE_AUTHOR("Liam Girdwood");
531 MODULE_DESCRIPTION("Sound Open Firmware (SOF) Core");
532 MODULE_LICENSE("Dual BSD/GPL");
533 MODULE_ALIAS("platform:sof-audio");
534 MODULE_IMPORT_NS(SND_SOC_SOF_CLIENT);