Linux Audio

Check our new training course

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