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