xref: /linux/sound/soc/sof/imx/imx-common.c (revision 4dd10fbbeb9c33709edc89916e19c03c780a0751)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2 //
3 // Copyright 2020-2025 NXP
4 //
5 // Common helpers for the audio DSP on i.MX8
6 
7 #include <linux/firmware/imx/dsp.h>
8 #include <linux/module.h>
9 #include <linux/of_address.h>
10 #include <linux/of_reserved_mem.h>
11 #include <linux/pm_domain.h>
12 #include <sound/sof/xtensa.h>
13 
14 #include "../ops.h"
15 
16 #include "imx-common.h"
17 
18 /**
19  * imx8_get_registers() - This function is called in case of DSP oops
20  * in order to gather information about the registers, filename and
21  * linenumber and stack.
22  * @sdev: SOF device
23  * @xoops: Stores information about registers.
24  * @panic_info: Stores information about filename and line number.
25  * @stack: Stores the stack dump.
26  * @stack_words: Size of the stack dump.
27  */
28 void imx8_get_registers(struct snd_sof_dev *sdev,
29 			struct sof_ipc_dsp_oops_xtensa *xoops,
30 			struct sof_ipc_panic_info *panic_info,
31 			u32 *stack, size_t stack_words)
32 {
33 	u32 offset = sdev->dsp_oops_offset;
34 
35 	/* first read registers */
36 	sof_mailbox_read(sdev, offset, xoops, sizeof(*xoops));
37 	xoops->plat_hdr.numaregs = 0;
38 
39 	/* then get panic info */
40 	if (xoops->arch_hdr.totalsize > EXCEPT_MAX_HDR_SIZE) {
41 		dev_err(sdev->dev, "invalid header size 0x%x. FW oops is bogus\n",
42 			xoops->arch_hdr.totalsize);
43 		return;
44 	}
45 	offset += xoops->arch_hdr.totalsize;
46 	sof_mailbox_read(sdev, offset, panic_info, sizeof(*panic_info));
47 
48 	/* then get the stack */
49 	offset += sizeof(*panic_info);
50 	sof_mailbox_read(sdev, offset, stack, stack_words * sizeof(u32));
51 }
52 
53 /**
54  * imx8_dump() - This function is called when a panic message is
55  * received from the firmware.
56  * @sdev: SOF device
57  * @flags: parameter not used but required by ops prototype
58  */
59 void imx8_dump(struct snd_sof_dev *sdev, u32 flags)
60 {
61 	struct sof_ipc_dsp_oops_xtensa xoops;
62 	struct sof_ipc_panic_info panic_info;
63 	u32 stack[IMX8_STACK_DUMP_SIZE];
64 	u32 status;
65 
66 	/* Get information about the panic status from the debug box area.
67 	 * Compute the trace point based on the status.
68 	 */
69 	sof_mailbox_read(sdev, sdev->debug_box.offset + 0x4, &status, 4);
70 
71 	/* Get information about the registers, the filename and line
72 	 * number and the stack.
73 	 */
74 	imx8_get_registers(sdev, &xoops, &panic_info, stack,
75 			   IMX8_STACK_DUMP_SIZE);
76 
77 	/* Print the information to the console */
78 	sof_print_oops_and_stack(sdev, KERN_ERR, status, status, &xoops,
79 				 &panic_info, stack, IMX8_STACK_DUMP_SIZE);
80 }
81 EXPORT_SYMBOL(imx8_dump);
82 
83 static void imx_handle_reply(struct imx_dsp_ipc *ipc)
84 {
85 	struct snd_sof_dev *sdev = imx_dsp_get_data(ipc);
86 
87 	guard(spinlock_irqsave)(&sdev->ipc_lock);
88 	snd_sof_ipc_process_reply(sdev, 0);
89 }
90 
91 static void imx_handle_request(struct imx_dsp_ipc *ipc)
92 {
93 	struct snd_sof_dev *sdev;
94 	u32 panic_code;
95 
96 	sdev = imx_dsp_get_data(ipc);
97 
98 	if (get_chip_info(sdev)->ipc_info.has_panic_code) {
99 		sof_mailbox_read(sdev, sdev->debug_box.offset + 0x4,
100 				 &panic_code,
101 				 sizeof(panic_code));
102 
103 		if ((panic_code & SOF_IPC_PANIC_MAGIC_MASK) == SOF_IPC_PANIC_MAGIC) {
104 			snd_sof_dsp_panic(sdev, panic_code, true);
105 			return;
106 		}
107 	}
108 
109 	snd_sof_ipc_msgs_rx(sdev);
110 }
111 
112 static struct imx_dsp_ops imx_ipc_ops = {
113 	.handle_reply = imx_handle_reply,
114 	.handle_request = imx_handle_request,
115 };
116 
117 static int imx_send_msg(struct snd_sof_dev *sdev, struct snd_sof_ipc_msg *msg)
118 {
119 	struct imx_common_data *common = sdev->pdata->hw_pdata;
120 
121 	sof_mailbox_write(sdev, sdev->host_box.offset, msg->msg_data, msg->msg_size);
122 	imx_dsp_ring_doorbell(common->ipc_handle, 0x0);
123 
124 	return 0;
125 }
126 
127 static int imx_get_bar_index(struct snd_sof_dev *sdev, u32 type)
128 {
129 	switch (type) {
130 	case SOF_FW_BLK_TYPE_IRAM:
131 	case SOF_FW_BLK_TYPE_SRAM:
132 		return type;
133 	default:
134 		return -EINVAL;
135 	}
136 }
137 
138 static int imx_get_mailbox_offset(struct snd_sof_dev *sdev)
139 {
140 	return get_chip_info(sdev)->ipc_info.boot_mbox_offset;
141 }
142 
143 static int imx_get_window_offset(struct snd_sof_dev *sdev, u32 id)
144 {
145 	return get_chip_info(sdev)->ipc_info.window_offset;
146 }
147 
148 static int imx_set_power_state(struct snd_sof_dev *sdev,
149 			       const struct sof_dsp_power_state *target)
150 {
151 	sdev->dsp_power_state = *target;
152 
153 	return 0;
154 }
155 
156 static int imx_common_resume(struct snd_sof_dev *sdev)
157 {
158 	struct imx_common_data *common;
159 	int ret, i;
160 
161 	common = sdev->pdata->hw_pdata;
162 
163 	ret = clk_bulk_prepare_enable(common->clk_num, common->clks);
164 	if (ret)
165 		dev_err(sdev->dev, "failed to enable clocks: %d\n", ret);
166 
167 	for (i = 0; i < DSP_MU_CHAN_NUM; i++)
168 		imx_dsp_request_channel(common->ipc_handle, i);
169 
170 	/* done. If need be, core will be started by SOF core immediately after */
171 	return 0;
172 }
173 
174 static int imx_common_suspend(struct snd_sof_dev *sdev)
175 {
176 	struct imx_common_data *common;
177 	int i, ret;
178 
179 	common = sdev->pdata->hw_pdata;
180 
181 	ret = imx_chip_core_shutdown(sdev);
182 	if (ret < 0) {
183 		dev_err(sdev->dev, "failed to shutdown core: %d\n", ret);
184 		return ret;
185 	}
186 
187 	for (i = 0; i < DSP_MU_CHAN_NUM; i++)
188 		imx_dsp_free_channel(common->ipc_handle, i);
189 
190 	clk_bulk_disable_unprepare(common->clk_num, common->clks);
191 
192 	return 0;
193 }
194 
195 static int imx_runtime_resume(struct snd_sof_dev *sdev)
196 {
197 	const struct sof_dsp_power_state target_state = {
198 		.state = SOF_DSP_PM_D0,
199 	};
200 	int ret;
201 
202 	ret = imx_common_resume(sdev);
203 	if (ret < 0) {
204 		dev_err(sdev->dev, "failed to runtime common resume: %d\n", ret);
205 		return ret;
206 	}
207 
208 	return snd_sof_dsp_set_power_state(sdev, &target_state);
209 }
210 
211 static int imx_resume(struct snd_sof_dev *sdev)
212 {
213 	const struct sof_dsp_power_state target_state = {
214 		.state = SOF_DSP_PM_D0,
215 	};
216 	int ret;
217 
218 	ret = imx_common_resume(sdev);
219 	if (ret < 0) {
220 		dev_err(sdev->dev, "failed to common resume: %d\n", ret);
221 		return ret;
222 	}
223 
224 	if (pm_runtime_suspended(sdev->dev)) {
225 		pm_runtime_disable(sdev->dev);
226 		pm_runtime_set_active(sdev->dev);
227 		pm_runtime_mark_last_busy(sdev->dev);
228 		pm_runtime_enable(sdev->dev);
229 		pm_runtime_idle(sdev->dev);
230 	}
231 
232 	return snd_sof_dsp_set_power_state(sdev, &target_state);
233 }
234 
235 static int imx_runtime_suspend(struct snd_sof_dev *sdev)
236 {
237 	const struct sof_dsp_power_state target_state = {
238 		.state = SOF_DSP_PM_D3,
239 	};
240 	int ret;
241 
242 	ret = imx_common_suspend(sdev);
243 	if (ret < 0)
244 		dev_err(sdev->dev, "failed to runtime common suspend: %d\n", ret);
245 
246 	return snd_sof_dsp_set_power_state(sdev, &target_state);
247 }
248 
249 static int imx_suspend(struct snd_sof_dev *sdev, unsigned int target_state)
250 {
251 	const struct sof_dsp_power_state target_power_state = {
252 		.state = target_state,
253 	};
254 	int ret;
255 
256 	if (!pm_runtime_suspended(sdev->dev)) {
257 		ret = imx_common_suspend(sdev);
258 		if (ret < 0) {
259 			dev_err(sdev->dev, "failed to common suspend: %d\n", ret);
260 			return ret;
261 		}
262 	}
263 
264 	return snd_sof_dsp_set_power_state(sdev, &target_power_state);
265 }
266 
267 static int imx_region_name_to_blk_type(const char *region_name)
268 {
269 	if (!strcmp(region_name, "iram"))
270 		return SOF_FW_BLK_TYPE_IRAM;
271 	else if (!strcmp(region_name, "dram"))
272 		return SOF_FW_BLK_TYPE_DRAM;
273 	else if (!strcmp(region_name, "sram"))
274 		return SOF_FW_BLK_TYPE_SRAM;
275 	else
276 		return -EINVAL;
277 }
278 
279 static int imx_parse_ioremap_memory(struct snd_sof_dev *sdev)
280 {
281 	const struct imx_chip_info *chip_info;
282 	struct platform_device *pdev;
283 	struct resource *res, _res;
284 	int i, blk_type, ret;
285 
286 	pdev = to_platform_device(sdev->dev);
287 	chip_info = get_chip_info(sdev);
288 
289 	for (i = 0; chip_info->memory[i].name; i++) {
290 		blk_type = imx_region_name_to_blk_type(chip_info->memory[i].name);
291 		if (blk_type < 0)
292 			return dev_err_probe(sdev->dev, blk_type,
293 					     "no blk type for region %s\n",
294 					     chip_info->memory[i].name);
295 
296 		if (!chip_info->memory[i].reserved) {
297 			res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
298 							   chip_info->memory[i].name);
299 			if (!res)
300 				return dev_err_probe(sdev->dev, -ENODEV,
301 						     "failed to fetch %s resource\n",
302 						     chip_info->memory[i].name);
303 
304 		} else {
305 			ret = of_reserved_mem_region_to_resource_byname(pdev->dev.of_node,
306 									chip_info->memory[i].name,
307 									&_res);
308 			if (ret < 0)
309 				return dev_err_probe(sdev->dev, ret,
310 						     "no valid entry for %s\n",
311 						     chip_info->memory[i].name);
312 			res = &_res;
313 		}
314 
315 		sdev->bar[blk_type] = devm_ioremap_resource(sdev->dev, res);
316 		if (IS_ERR(sdev->bar[blk_type]))
317 			return dev_err_probe(sdev->dev,
318 					     PTR_ERR(sdev->bar[blk_type]),
319 					     "failed to ioremap %s region\n",
320 					     chip_info->memory[i].name);
321 	}
322 
323 	return 0;
324 }
325 
326 static void imx_unregister_action(void *data)
327 {
328 	struct imx_common_data *common;
329 	struct snd_sof_dev *sdev;
330 
331 	sdev = data;
332 	common = sdev->pdata->hw_pdata;
333 
334 	if (get_chip_info(sdev)->has_dma_reserved)
335 		of_reserved_mem_device_release(sdev->dev);
336 
337 	platform_device_unregister(common->ipc_dev);
338 }
339 
340 static int imx_probe(struct snd_sof_dev *sdev)
341 {
342 	struct dev_pm_domain_attach_data domain_data = {
343 		.pd_names = NULL, /* no filtering */
344 		.pd_flags = PD_FLAG_DEV_LINK_ON,
345 	};
346 	struct imx_common_data *common;
347 	struct platform_device *pdev;
348 	int ret;
349 
350 	pdev = to_platform_device(sdev->dev);
351 
352 	common = devm_kzalloc_obj(sdev->dev, *common);
353 	if (!common)
354 		return -ENOMEM;
355 
356 	sdev->pdata->hw_pdata = common;
357 
358 	common->ipc_dev = platform_device_register_data(sdev->dev, "imx-dsp",
359 							PLATFORM_DEVID_NONE,
360 							pdev, sizeof(*pdev));
361 	if (IS_ERR(common->ipc_dev))
362 		return dev_err_probe(sdev->dev, PTR_ERR(common->ipc_dev),
363 				     "failed to create IPC device\n");
364 
365 	if (get_chip_info(sdev)->has_dma_reserved) {
366 		ret = of_reserved_mem_device_init_by_name(sdev->dev,
367 							  pdev->dev.of_node,
368 							  "dma");
369 		if (ret) {
370 			platform_device_unregister(common->ipc_dev);
371 
372 			return dev_err_probe(sdev->dev, ret,
373 					     "failed to bind DMA region\n");
374 		}
375 	}
376 
377 	/* let the devres API take care of the cleanup */
378 	ret = devm_add_action_or_reset(sdev->dev,
379 				       imx_unregister_action,
380 				       sdev);
381 	if (ret)
382 		return ret;
383 
384 	common->ipc_handle = dev_get_drvdata(&common->ipc_dev->dev);
385 	if (!common->ipc_handle)
386 		return dev_err_probe(sdev->dev, -EPROBE_DEFER,
387 				     "failed to fetch IPC handle\n");
388 
389 	ret = imx_parse_ioremap_memory(sdev);
390 	if (ret < 0)
391 		return dev_err_probe(sdev->dev, ret,
392 				     "failed to parse/ioremap memory regions\n");
393 
394 	if (!sdev->dev->pm_domain) {
395 		ret = devm_pm_domain_attach_list(sdev->dev,
396 						 &domain_data, &common->pd_list);
397 		if (ret < 0)
398 			return dev_err_probe(sdev->dev, ret, "failed to attach PDs\n");
399 	}
400 
401 	ret = devm_clk_bulk_get_all(sdev->dev, &common->clks);
402 	if (ret < 0)
403 		return dev_err_probe(sdev->dev, ret, "failed to fetch clocks\n");
404 	common->clk_num = ret;
405 
406 	ret = clk_bulk_prepare_enable(common->clk_num, common->clks);
407 	if (ret < 0)
408 		return dev_err_probe(sdev->dev, ret, "failed to enable clocks\n");
409 
410 	common->ipc_handle->ops = &imx_ipc_ops;
411 	imx_dsp_set_data(common->ipc_handle, sdev);
412 
413 	sdev->num_cores = 1;
414 	sdev->mailbox_bar = SOF_FW_BLK_TYPE_SRAM;
415 	sdev->dsp_box.offset = get_chip_info(sdev)->ipc_info.boot_mbox_offset;
416 
417 	return imx_chip_probe(sdev);
418 }
419 
420 static void imx_remove(struct snd_sof_dev *sdev)
421 {
422 	struct imx_common_data *common;
423 	int ret;
424 
425 	common = sdev->pdata->hw_pdata;
426 
427 	if (!pm_runtime_suspended(sdev->dev)) {
428 		ret = imx_chip_core_shutdown(sdev);
429 		if (ret < 0)
430 			dev_err(sdev->dev, "failed to shutdown core: %d\n", ret);
431 
432 		clk_bulk_disable_unprepare(common->clk_num, common->clks);
433 	}
434 }
435 
436 const struct snd_sof_dsp_ops sof_imx_ops = {
437 	.probe = imx_probe,
438 	.remove = imx_remove,
439 
440 	.run = imx_chip_core_kick,
441 	.reset = imx_chip_core_reset,
442 
443 	.block_read = sof_block_read,
444 	.block_write = sof_block_write,
445 
446 	.mailbox_read = sof_mailbox_read,
447 	.mailbox_write = sof_mailbox_write,
448 
449 	.send_msg = imx_send_msg,
450 	.get_mailbox_offset = imx_get_mailbox_offset,
451 	.get_window_offset = imx_get_window_offset,
452 
453 	.ipc_msg_data = sof_ipc_msg_data,
454 	.set_stream_data_offset = sof_set_stream_data_offset,
455 
456 	.get_bar_index = imx_get_bar_index,
457 	.load_firmware = snd_sof_load_firmware_memcpy,
458 
459 	.debugfs_add_region_item = snd_sof_debugfs_add_region_item_iomem,
460 
461 	.pcm_open = sof_stream_pcm_open,
462 	.pcm_close = sof_stream_pcm_close,
463 
464 	.runtime_suspend = imx_runtime_suspend,
465 	.runtime_resume = imx_runtime_resume,
466 	.suspend = imx_suspend,
467 	.resume = imx_resume,
468 
469 	.set_power_state = imx_set_power_state,
470 
471 	.hw_info = SNDRV_PCM_INFO_MMAP |
472 		SNDRV_PCM_INFO_MMAP_VALID |
473 		SNDRV_PCM_INFO_INTERLEAVED |
474 		SNDRV_PCM_INFO_PAUSE |
475 		SNDRV_PCM_INFO_BATCH |
476 		SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
477 };
478 EXPORT_SYMBOL(sof_imx_ops);
479 
480 MODULE_LICENSE("Dual BSD/GPL");
481 MODULE_DESCRIPTION("SOF helpers for IMX platforms");
482