1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
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.
6 // Copyright(c) 2018 Intel Corporation. All rights reserved.
8 // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
11 #include <linux/firmware.h>
12 #include <linux/module.h>
13 #include <asm/unaligned.h>
14 #include <sound/soc.h>
15 #include <sound/sof.h>
19 /* see SOF_DBG_ flags */
21 module_param_named(sof_debug, sof_core_debug, int, 0444);
22 MODULE_PARM_DESC(sof_debug, "SOF core debug options (0x0 all off)");
24 /* SOF defaults if not provided by the platform in ms */
25 #define TIMEOUT_DEFAULT_IPC_MS 500
26 #define TIMEOUT_DEFAULT_BOOT_MS 2000
29 * Generic object lookup APIs.
32 struct snd_sof_pcm *snd_sof_find_spcm_name(struct snd_sof_dev *sdev,
35 struct snd_sof_pcm *spcm;
37 list_for_each_entry(spcm, &sdev->pcm_list, list) {
38 /* match with PCM dai name */
39 if (strcmp(spcm->pcm.dai_name, name) == 0)
42 /* match with playback caps name if set */
43 if (*spcm->pcm.caps[0].name &&
44 !strcmp(spcm->pcm.caps[0].name, name))
47 /* match with capture caps name if set */
48 if (*spcm->pcm.caps[1].name &&
49 !strcmp(spcm->pcm.caps[1].name, name))
56 struct snd_sof_pcm *snd_sof_find_spcm_comp(struct snd_sof_dev *sdev,
60 struct snd_sof_pcm *spcm;
62 list_for_each_entry(spcm, &sdev->pcm_list, list) {
63 if (spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].comp_id == comp_id) {
64 *direction = SNDRV_PCM_STREAM_PLAYBACK;
67 if (spcm->stream[SNDRV_PCM_STREAM_CAPTURE].comp_id == comp_id) {
68 *direction = SNDRV_PCM_STREAM_CAPTURE;
76 struct snd_sof_pcm *snd_sof_find_spcm_pcm_id(struct snd_sof_dev *sdev,
79 struct snd_sof_pcm *spcm;
81 list_for_each_entry(spcm, &sdev->pcm_list, list) {
82 if (le32_to_cpu(spcm->pcm.pcm_id) == pcm_id)
89 struct snd_sof_widget *snd_sof_find_swidget(struct snd_sof_dev *sdev,
92 struct snd_sof_widget *swidget;
94 list_for_each_entry(swidget, &sdev->widget_list, list) {
95 if (strcmp(name, swidget->widget->name) == 0)
102 /* find widget by stream name and direction */
103 struct snd_sof_widget *snd_sof_find_swidget_sname(struct snd_sof_dev *sdev,
104 const char *pcm_name, int dir)
106 struct snd_sof_widget *swidget;
107 enum snd_soc_dapm_type type;
109 if (dir == SNDRV_PCM_STREAM_PLAYBACK)
110 type = snd_soc_dapm_aif_in;
112 type = snd_soc_dapm_aif_out;
114 list_for_each_entry(swidget, &sdev->widget_list, list) {
115 if (!strcmp(pcm_name, swidget->widget->sname) && swidget->id == type)
122 struct snd_sof_dai *snd_sof_find_dai(struct snd_sof_dev *sdev,
125 struct snd_sof_dai *dai;
127 list_for_each_entry(dai, &sdev->dai_list, list) {
128 if (dai->name && (strcmp(name, dai->name) == 0))
136 * FW Panic/fault handling.
139 struct sof_panic_msg {
144 /* standard FW panic types */
145 static const struct sof_panic_msg panic_msg[] = {
146 {SOF_IPC_PANIC_MEM, "out of memory"},
147 {SOF_IPC_PANIC_WORK, "work subsystem init failed"},
148 {SOF_IPC_PANIC_IPC, "IPC subsystem init failed"},
149 {SOF_IPC_PANIC_ARCH, "arch init failed"},
150 {SOF_IPC_PANIC_PLATFORM, "platform init failed"},
151 {SOF_IPC_PANIC_TASK, "scheduler init failed"},
152 {SOF_IPC_PANIC_EXCEPTION, "runtime exception"},
153 {SOF_IPC_PANIC_DEADLOCK, "deadlock"},
154 {SOF_IPC_PANIC_STACK, "stack overflow"},
155 {SOF_IPC_PANIC_IDLE, "can't enter idle"},
156 {SOF_IPC_PANIC_WFI, "invalid wait state"},
157 {SOF_IPC_PANIC_ASSERT, "assertion failed"},
161 * helper to be called from .dbg_dump callbacks. No error code is
162 * provided, it's left as an exercise for the caller of .dbg_dump
163 * (typically IPC or loader)
165 void snd_sof_get_status(struct snd_sof_dev *sdev, u32 panic_code,
166 u32 tracep_code, void *oops,
167 struct sof_ipc_panic_info *panic_info,
168 void *stack, size_t stack_words)
173 /* is firmware dead ? */
174 if ((panic_code & SOF_IPC_PANIC_MAGIC_MASK) != SOF_IPC_PANIC_MAGIC) {
175 dev_err(sdev->dev, "error: unexpected fault 0x%8.8x trace 0x%8.8x\n",
176 panic_code, tracep_code);
177 return; /* no fault ? */
180 code = panic_code & (SOF_IPC_PANIC_MAGIC_MASK | SOF_IPC_PANIC_CODE_MASK);
182 for (i = 0; i < ARRAY_SIZE(panic_msg); i++) {
183 if (panic_msg[i].id == code) {
184 dev_err(sdev->dev, "error: %s\n", panic_msg[i].msg);
185 dev_err(sdev->dev, "error: trace point %8.8x\n",
192 dev_err(sdev->dev, "error: unknown reason %8.8x\n", panic_code);
193 dev_err(sdev->dev, "error: trace point %8.8x\n", tracep_code);
196 dev_err(sdev->dev, "error: panic at %s:%d\n",
197 panic_info->filename, panic_info->linenum);
198 sof_oops(sdev, oops);
199 sof_stack(sdev, oops, stack, stack_words);
201 EXPORT_SYMBOL(snd_sof_get_status);
204 * Generic buffer page table creation.
205 * Take the each physical page address and drop the least significant unused
206 * bits from each (based on PAGE_SIZE). Then pack valid page address bits
207 * into compressed page table.
210 int snd_sof_create_page_table(struct snd_sof_dev *sdev,
211 struct snd_dma_buffer *dmab,
212 unsigned char *page_table, size_t size)
216 pages = snd_sgbuf_aligned_pages(size);
218 dev_dbg(sdev->dev, "generating page table for %p size 0x%zx pages %d\n",
219 dmab->area, size, pages);
221 for (i = 0; i < pages; i++) {
223 * The number of valid address bits for each page is 20.
224 * idx determines the byte position within page_table
225 * where the current page's address is stored
226 * in the compressed page_table.
227 * This can be calculated by multiplying the page number by 2.5.
229 u32 idx = (5 * i) >> 1;
230 u32 pfn = snd_sgbuf_get_addr(dmab, i * PAGE_SIZE) >> PAGE_SHIFT;
233 dev_vdbg(sdev->dev, "pfn i %i idx %d pfn %x\n", i, idx, pfn);
235 pg_table = (u8 *)(page_table + idx);
238 * pagetable compression:
239 * byte 0 byte 1 byte 2 byte 3 byte 4 byte 5
240 * ___________pfn 0__________ __________pfn 1___________ _pfn 2...
241 * .... .... .... .... .... .... .... .... .... .... ....
243 * 1. set current location to 0, PFN index i to 0
244 * 2. put pfn[i] at current location in Little Endian byte order
245 * 3. calculate an intermediate value as
246 * x = (pfn[i+1] << 4) | (pfn[i] & 0xf)
247 * 4. put x at offset (current location + 2) in LE byte order
248 * 5. increment current location by 5 bytes, increment i by 2
252 put_unaligned_le32((pg_table[0] & 0xf) | pfn << 4,
255 put_unaligned_le32(pfn, pg_table);
262 * SOF Driver enumeration.
264 static int sof_machine_check(struct snd_sof_dev *sdev)
266 struct snd_sof_pdata *plat_data = sdev->pdata;
267 #if IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC)
268 struct snd_soc_acpi_mach *machine;
272 if (plat_data->machine)
275 #if !IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC)
276 dev_err(sdev->dev, "error: no matching ASoC machine driver found - aborting probe\n");
279 /* fallback to nocodec mode */
280 dev_warn(sdev->dev, "No ASoC machine driver found - using nocodec\n");
281 machine = devm_kzalloc(sdev->dev, sizeof(*machine), GFP_KERNEL);
285 ret = sof_nocodec_setup(sdev->dev, plat_data, machine,
286 plat_data->desc, plat_data->desc->ops);
290 plat_data->machine = machine;
296 static int sof_probe_continue(struct snd_sof_dev *sdev)
298 struct snd_sof_pdata *plat_data = sdev->pdata;
299 const char *drv_name;
304 /* probe the DSP hardware */
305 ret = snd_sof_probe(sdev);
307 dev_err(sdev->dev, "error: failed to probe DSP %d\n", ret);
311 /* check machine info */
312 ret = sof_machine_check(sdev);
314 dev_err(sdev->dev, "error: failed to get machine info %d\n",
319 /* set up platform component driver */
320 snd_sof_new_platform_drv(sdev);
322 /* register any debug/trace capabilities */
323 ret = snd_sof_dbg_init(sdev);
326 * debugfs issues are suppressed in snd_sof_dbg_init() since
327 * we cannot rely on debugfs
328 * here we trap errors due to memory allocation only.
330 dev_err(sdev->dev, "error: failed to init DSP trace/debug %d\n",
336 sdev->ipc = snd_sof_ipc_init(sdev);
338 dev_err(sdev->dev, "error: failed to init DSP IPC %d\n", ret);
342 /* load the firmware */
343 ret = snd_sof_load_firmware(sdev);
345 dev_err(sdev->dev, "error: failed to load DSP firmware %d\n",
350 /* boot the firmware */
351 ret = snd_sof_run_firmware(sdev);
353 dev_err(sdev->dev, "error: failed to boot DSP firmware %d\n",
358 if (IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_FIRMWARE_TRACE) ||
359 (sof_core_debug & SOF_DBG_ENABLE_TRACE)) {
360 sdev->dtrace_is_supported = true;
363 ret = snd_sof_init_trace(sdev);
367 "warning: failed to initialize trace %d\n",
371 dev_dbg(sdev->dev, "SOF firmware trace disabled\n");
374 /* hereafter all FW boot flows are for PM reasons */
375 sdev->first_boot = false;
377 /* now register audio DSP platform driver and dai */
378 ret = devm_snd_soc_register_component(sdev->dev, &sdev->plat_drv,
380 sof_ops(sdev)->num_drv);
383 "error: failed to register DSP DAI driver %d\n", ret);
387 drv_name = plat_data->machine->drv_name;
388 mach = (const void *)plat_data->machine;
389 size = sizeof(*plat_data->machine);
391 /* register machine driver, pass machine info as pdata */
392 plat_data->pdev_mach =
393 platform_device_register_data(sdev->dev, drv_name,
394 PLATFORM_DEVID_NONE, mach, size);
396 if (IS_ERR(plat_data->pdev_mach)) {
397 ret = PTR_ERR(plat_data->pdev_mach);
401 dev_dbg(sdev->dev, "created machine %s\n",
402 dev_name(&plat_data->pdev_mach->dev));
404 if (plat_data->sof_probe_complete)
405 plat_data->sof_probe_complete(sdev->dev);
409 #if !IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE)
411 snd_sof_fw_unload(sdev);
413 snd_sof_ipc_free(sdev);
415 snd_sof_free_debug(sdev);
417 snd_sof_remove(sdev);
421 * when the probe_continue is handled in a work queue, the
422 * probe does not fail so we don't release resources here.
423 * They will be released with an explicit call to
424 * snd_sof_device_remove() when the PCI/ACPI device is removed
437 static void sof_probe_work(struct work_struct *work)
439 struct snd_sof_dev *sdev =
440 container_of(work, struct snd_sof_dev, probe_work);
443 ret = sof_probe_continue(sdev);
445 /* errors cannot be propagated, log */
446 dev_err(sdev->dev, "error: %s failed err: %d\n", __func__, ret);
450 int snd_sof_device_probe(struct device *dev, struct snd_sof_pdata *plat_data)
452 struct snd_sof_dev *sdev;
454 sdev = devm_kzalloc(dev, sizeof(*sdev), GFP_KERNEL);
458 /* initialize sof device */
461 sdev->pdata = plat_data;
462 sdev->first_boot = true;
463 dev_set_drvdata(dev, sdev);
465 /* check all mandatory ops */
466 if (!sof_ops(sdev) || !sof_ops(sdev)->probe || !sof_ops(sdev)->run ||
467 !sof_ops(sdev)->block_read || !sof_ops(sdev)->block_write ||
468 !sof_ops(sdev)->send_msg || !sof_ops(sdev)->load_firmware ||
469 !sof_ops(sdev)->ipc_msg_data || !sof_ops(sdev)->ipc_pcm_params)
472 INIT_LIST_HEAD(&sdev->pcm_list);
473 INIT_LIST_HEAD(&sdev->kcontrol_list);
474 INIT_LIST_HEAD(&sdev->widget_list);
475 INIT_LIST_HEAD(&sdev->dai_list);
476 INIT_LIST_HEAD(&sdev->route_list);
477 spin_lock_init(&sdev->ipc_lock);
478 spin_lock_init(&sdev->hw_lock);
480 if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE))
481 INIT_WORK(&sdev->probe_work, sof_probe_work);
483 /* set default timeouts if none provided */
484 if (plat_data->desc->ipc_timeout == 0)
485 sdev->ipc_timeout = TIMEOUT_DEFAULT_IPC_MS;
487 sdev->ipc_timeout = plat_data->desc->ipc_timeout;
488 if (plat_data->desc->boot_timeout == 0)
489 sdev->boot_timeout = TIMEOUT_DEFAULT_BOOT_MS;
491 sdev->boot_timeout = plat_data->desc->boot_timeout;
493 if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE)) {
494 schedule_work(&sdev->probe_work);
498 return sof_probe_continue(sdev);
500 EXPORT_SYMBOL(snd_sof_device_probe);
502 int snd_sof_device_remove(struct device *dev)
504 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
505 struct snd_sof_pdata *pdata = sdev->pdata;
507 if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE))
508 cancel_work_sync(&sdev->probe_work);
510 snd_sof_fw_unload(sdev);
511 snd_sof_ipc_free(sdev);
512 snd_sof_free_debug(sdev);
513 snd_sof_free_trace(sdev);
516 * Unregister machine driver. This will unbind the snd_card which
517 * will remove the component driver and unload the topology
518 * before freeing the snd_card.
520 if (!IS_ERR_OR_NULL(pdata->pdev_mach))
521 platform_device_unregister(pdata->pdev_mach);
524 * Unregistering the machine driver results in unloading the topology.
525 * Some widgets, ex: scheduler, attempt to power down the core they are
526 * scheduled on, when they are unloaded. Therefore, the DSP must be
527 * removed only after the topology has been unloaded.
529 snd_sof_remove(sdev);
531 /* release firmware */
532 release_firmware(pdata->fw);
537 EXPORT_SYMBOL(snd_sof_device_remove);
539 MODULE_AUTHOR("Liam Girdwood");
540 MODULE_DESCRIPTION("Sound Open Firmware (SOF) Core");
541 MODULE_LICENSE("Dual BSD/GPL");
542 MODULE_ALIAS("platform:sof-audio");