xref: /linux/sound/soc/sof/core.c (revision 52a9dab6d892763b2a8334a568bd4e2c1a6fde66)
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 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_PROBES)
18 #include "sof-probes.h"
19 #endif
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 /*
126  *			FW Boot State Transition Diagram
127  *
128  *    +----------------------------------------------------------------------+
129  *    |									     |
130  * ------------------	     ------------------				     |
131  * |		    |	     |		      |				     |
132  * |   BOOT_FAILED  |<-------|  READY_FAILED  |				     |
133  * |		    |<--+    |	              |	   ------------------	     |
134  * ------------------	|    ------------------	   |		    |	     |
135  *	^		|	    ^		   |	CRASHED	    |---+    |
136  *	|		|	    |		   |		    |	|    |
137  * (FW Boot Timeout)	|	(FW_READY FAIL)	   ------------------	|    |
138  *	|		|	    |		     ^			|    |
139  *	|		|	    |		     |(DSP Panic)	|    |
140  * ------------------	|	    |		   ------------------	|    |
141  * |		    |	|	    |		   |		    |	|    |
142  * |   IN_PROGRESS  |---------------+------------->|    COMPLETE    |	|    |
143  * |		    | (FW Boot OK)   (FW_READY OK) |		    |	|    |
144  * ------------------	|			   ------------------	|    |
145  *	^		|				|		|    |
146  *	|		|				|		|    |
147  * (FW Loading OK)	|			(System Suspend/Runtime Suspend)
148  *	|		|				|		|    |
149  *	|	(FW Loading Fail)			|		|    |
150  * ------------------	|	------------------	|		|    |
151  * |		    |	|	|		 |<-----+		|    |
152  * |   PREPARE	    |---+	|   NOT_STARTED  |<---------------------+    |
153  * |		    |		|		 |<--------------------------+
154  * ------------------		------------------
155  *    |	    ^			    |	   ^
156  *    |	    |			    |	   |
157  *    |	    +-----------------------+	   |
158  *    |		(DSP Probe OK)		   |
159  *    |					   |
160  *    |					   |
161  *    +------------------------------------+
162  *	(System Suspend/Runtime Suspend)
163  */
164 
165 static int sof_probe_continue(struct snd_sof_dev *sdev)
166 {
167 	struct snd_sof_pdata *plat_data = sdev->pdata;
168 	int ret;
169 
170 	/* probe the DSP hardware */
171 	ret = snd_sof_probe(sdev);
172 	if (ret < 0) {
173 		dev_err(sdev->dev, "error: failed to probe DSP %d\n", ret);
174 		return ret;
175 	}
176 
177 	sof_set_fw_state(sdev, SOF_FW_BOOT_PREPARE);
178 
179 	/* check machine info */
180 	ret = sof_machine_check(sdev);
181 	if (ret < 0) {
182 		dev_err(sdev->dev, "error: failed to get machine info %d\n",
183 			ret);
184 		goto dsp_err;
185 	}
186 
187 	/* set up platform component driver */
188 	snd_sof_new_platform_drv(sdev);
189 
190 	/* register any debug/trace capabilities */
191 	ret = snd_sof_dbg_init(sdev);
192 	if (ret < 0) {
193 		/*
194 		 * debugfs issues are suppressed in snd_sof_dbg_init() since
195 		 * we cannot rely on debugfs
196 		 * here we trap errors due to memory allocation only.
197 		 */
198 		dev_err(sdev->dev, "error: failed to init DSP trace/debug %d\n",
199 			ret);
200 		goto dbg_err;
201 	}
202 
203 	/* init the IPC */
204 	sdev->ipc = snd_sof_ipc_init(sdev);
205 	if (!sdev->ipc) {
206 		ret = -ENOMEM;
207 		dev_err(sdev->dev, "error: failed to init DSP IPC %d\n", ret);
208 		goto ipc_err;
209 	}
210 
211 	/* load the firmware */
212 	ret = snd_sof_load_firmware(sdev);
213 	if (ret < 0) {
214 		dev_err(sdev->dev, "error: failed to load DSP firmware %d\n",
215 			ret);
216 		sof_set_fw_state(sdev, SOF_FW_BOOT_FAILED);
217 		goto fw_load_err;
218 	}
219 
220 	sof_set_fw_state(sdev, SOF_FW_BOOT_IN_PROGRESS);
221 
222 	/*
223 	 * Boot the firmware. The FW boot status will be modified
224 	 * in snd_sof_run_firmware() depending on the outcome.
225 	 */
226 	ret = snd_sof_run_firmware(sdev);
227 	if (ret < 0) {
228 		dev_err(sdev->dev, "error: failed to boot DSP firmware %d\n",
229 			ret);
230 		sof_set_fw_state(sdev, SOF_FW_BOOT_FAILED);
231 		goto fw_run_err;
232 	}
233 
234 	if (sof_debug_check_flag(SOF_DBG_ENABLE_TRACE)) {
235 		sdev->dtrace_is_supported = true;
236 
237 		/* init DMA trace */
238 		ret = snd_sof_init_trace(sdev);
239 		if (ret < 0) {
240 			/* non fatal */
241 			dev_warn(sdev->dev,
242 				 "warning: failed to initialize trace %d\n",
243 				 ret);
244 		}
245 	} else {
246 		dev_dbg(sdev->dev, "SOF firmware trace disabled\n");
247 	}
248 
249 	/* hereafter all FW boot flows are for PM reasons */
250 	sdev->first_boot = false;
251 
252 	/* now register audio DSP platform driver and dai */
253 	ret = devm_snd_soc_register_component(sdev->dev, &sdev->plat_drv,
254 					      sof_ops(sdev)->drv,
255 					      sof_ops(sdev)->num_drv);
256 	if (ret < 0) {
257 		dev_err(sdev->dev,
258 			"error: failed to register DSP DAI driver %d\n", ret);
259 		goto fw_trace_err;
260 	}
261 
262 	ret = snd_sof_machine_register(sdev, plat_data);
263 	if (ret < 0) {
264 		dev_err(sdev->dev,
265 			"error: failed to register machine driver %d\n", ret);
266 		goto fw_trace_err;
267 	}
268 
269 	/*
270 	 * Some platforms in SOF, ex: BYT, may not have their platform PM
271 	 * callbacks set. Increment the usage count so as to
272 	 * prevent the device from entering runtime suspend.
273 	 */
274 	if (!sof_ops(sdev)->runtime_suspend || !sof_ops(sdev)->runtime_resume)
275 		pm_runtime_get_noresume(sdev->dev);
276 
277 	if (plat_data->sof_probe_complete)
278 		plat_data->sof_probe_complete(sdev->dev);
279 
280 	sdev->probe_completed = true;
281 
282 	return 0;
283 
284 fw_trace_err:
285 	snd_sof_free_trace(sdev);
286 fw_run_err:
287 	snd_sof_fw_unload(sdev);
288 fw_load_err:
289 	snd_sof_ipc_free(sdev);
290 ipc_err:
291 dbg_err:
292 	snd_sof_free_debug(sdev);
293 dsp_err:
294 	snd_sof_remove(sdev);
295 
296 	/* all resources freed, update state to match */
297 	sof_set_fw_state(sdev, SOF_FW_BOOT_NOT_STARTED);
298 	sdev->first_boot = true;
299 
300 	return ret;
301 }
302 
303 static void sof_probe_work(struct work_struct *work)
304 {
305 	struct snd_sof_dev *sdev =
306 		container_of(work, struct snd_sof_dev, probe_work);
307 	int ret;
308 
309 	ret = sof_probe_continue(sdev);
310 	if (ret < 0) {
311 		/* errors cannot be propagated, log */
312 		dev_err(sdev->dev, "error: %s failed err: %d\n", __func__, ret);
313 	}
314 }
315 
316 int snd_sof_device_probe(struct device *dev, struct snd_sof_pdata *plat_data)
317 {
318 	struct snd_sof_dev *sdev;
319 
320 	sdev = devm_kzalloc(dev, sizeof(*sdev), GFP_KERNEL);
321 	if (!sdev)
322 		return -ENOMEM;
323 
324 	/* initialize sof device */
325 	sdev->dev = dev;
326 
327 	/* initialize default DSP power state */
328 	sdev->dsp_power_state.state = SOF_DSP_PM_D0;
329 
330 	sdev->pdata = plat_data;
331 	sdev->first_boot = true;
332 	sof_set_fw_state(sdev, SOF_FW_BOOT_NOT_STARTED);
333 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_PROBES)
334 	sdev->extractor_stream_tag = SOF_PROBE_INVALID_NODE_ID;
335 #endif
336 	dev_set_drvdata(dev, sdev);
337 
338 	/* check all mandatory ops */
339 	if (!sof_ops(sdev) || !sof_ops(sdev)->probe || !sof_ops(sdev)->run ||
340 	    !sof_ops(sdev)->block_read || !sof_ops(sdev)->block_write ||
341 	    !sof_ops(sdev)->send_msg || !sof_ops(sdev)->load_firmware ||
342 	    !sof_ops(sdev)->ipc_msg_data || !sof_ops(sdev)->ipc_pcm_params ||
343 	    !sof_ops(sdev)->fw_ready) {
344 		dev_err(dev, "error: missing mandatory ops\n");
345 		return -EINVAL;
346 	}
347 
348 	INIT_LIST_HEAD(&sdev->pcm_list);
349 	INIT_LIST_HEAD(&sdev->kcontrol_list);
350 	INIT_LIST_HEAD(&sdev->widget_list);
351 	INIT_LIST_HEAD(&sdev->dai_list);
352 	INIT_LIST_HEAD(&sdev->route_list);
353 	spin_lock_init(&sdev->ipc_lock);
354 	spin_lock_init(&sdev->hw_lock);
355 	mutex_init(&sdev->power_state_access);
356 
357 	/* set default timeouts if none provided */
358 	if (plat_data->desc->ipc_timeout == 0)
359 		sdev->ipc_timeout = TIMEOUT_DEFAULT_IPC_MS;
360 	else
361 		sdev->ipc_timeout = plat_data->desc->ipc_timeout;
362 	if (plat_data->desc->boot_timeout == 0)
363 		sdev->boot_timeout = TIMEOUT_DEFAULT_BOOT_MS;
364 	else
365 		sdev->boot_timeout = plat_data->desc->boot_timeout;
366 
367 	if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE)) {
368 		INIT_WORK(&sdev->probe_work, sof_probe_work);
369 		schedule_work(&sdev->probe_work);
370 		return 0;
371 	}
372 
373 	return sof_probe_continue(sdev);
374 }
375 EXPORT_SYMBOL(snd_sof_device_probe);
376 
377 bool snd_sof_device_probe_completed(struct device *dev)
378 {
379 	struct snd_sof_dev *sdev = dev_get_drvdata(dev);
380 
381 	return sdev->probe_completed;
382 }
383 EXPORT_SYMBOL(snd_sof_device_probe_completed);
384 
385 int snd_sof_device_remove(struct device *dev)
386 {
387 	struct snd_sof_dev *sdev = dev_get_drvdata(dev);
388 	struct snd_sof_pdata *pdata = sdev->pdata;
389 	int ret;
390 
391 	if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE))
392 		cancel_work_sync(&sdev->probe_work);
393 
394 	/*
395 	 * Unregister machine driver. This will unbind the snd_card which
396 	 * will remove the component driver and unload the topology
397 	 * before freeing the snd_card.
398 	 */
399 	snd_sof_machine_unregister(sdev, pdata);
400 
401 	if (sdev->fw_state > SOF_FW_BOOT_NOT_STARTED) {
402 		snd_sof_free_trace(sdev);
403 		ret = snd_sof_dsp_power_down_notify(sdev);
404 		if (ret < 0)
405 			dev_warn(dev, "error: %d failed to prepare DSP for device removal",
406 				 ret);
407 
408 		snd_sof_ipc_free(sdev);
409 		snd_sof_free_debug(sdev);
410 	}
411 
412 	/*
413 	 * Unregistering the machine driver results in unloading the topology.
414 	 * Some widgets, ex: scheduler, attempt to power down the core they are
415 	 * scheduled on, when they are unloaded. Therefore, the DSP must be
416 	 * removed only after the topology has been unloaded.
417 	 */
418 	if (sdev->fw_state > SOF_FW_BOOT_NOT_STARTED)
419 		snd_sof_remove(sdev);
420 
421 	/* release firmware */
422 	snd_sof_fw_unload(sdev);
423 
424 	return 0;
425 }
426 EXPORT_SYMBOL(snd_sof_device_remove);
427 
428 int snd_sof_device_shutdown(struct device *dev)
429 {
430 	struct snd_sof_dev *sdev = dev_get_drvdata(dev);
431 
432 	if (IS_ENABLED(CONFIG_SND_SOC_SOF_PROBE_WORK_QUEUE))
433 		cancel_work_sync(&sdev->probe_work);
434 
435 	if (sdev->fw_state == SOF_FW_BOOT_COMPLETE)
436 		return snd_sof_shutdown(sdev);
437 
438 	return 0;
439 }
440 EXPORT_SYMBOL(snd_sof_device_shutdown);
441 
442 MODULE_AUTHOR("Liam Girdwood");
443 MODULE_DESCRIPTION("Sound Open Firmware (SOF) Core");
444 MODULE_LICENSE("Dual BSD/GPL");
445 MODULE_ALIAS("platform:sof-audio");
446