xref: /linux/sound/soc/sof/amd/acp-common.c (revision 4dd10fbbeb9c33709edc89916e19c03c780a0751)
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) 2022 Advanced Micro Devices, Inc.
7 //
8 // Authors: Ajit Kumar Pandey <AjitKumar.Pandey@amd.com>
9 //	    V sujith kumar Reddy <Vsujithkumar.Reddy@amd.com>
10 
11 /* ACP-specific Common code */
12 
13 #include "../sof-priv.h"
14 #include "../sof-audio.h"
15 #include "../ops.h"
16 #include "acp.h"
17 #include "acp-dsp-offset.h"
18 #include <sound/sof/xtensa.h>
19 #include "../../amd/acp/soc_amd_sdw_common.h"
20 
21 /**
22  * amd_sof_ipc_dump() - This function is called when IPC tx times out.
23  * @sdev: SOF device.
24  */
25 void amd_sof_ipc_dump(struct snd_sof_dev *sdev)
26 {
27 	const struct sof_amd_acp_desc *desc = get_chip_info(sdev->pdata);
28 	u32 base = desc->dsp_intr_base;
29 	u32 dsp_msg_write = sdev->debug_box.offset +
30 			    offsetof(struct scratch_ipc_conf, sof_dsp_msg_write);
31 	u32 dsp_ack_write = sdev->debug_box.offset +
32 			    offsetof(struct scratch_ipc_conf, sof_dsp_ack_write);
33 	u32 host_msg_write = sdev->debug_box.offset +
34 			     offsetof(struct scratch_ipc_conf, sof_host_msg_write);
35 	u32 host_ack_write = sdev->debug_box.offset +
36 			     offsetof(struct scratch_ipc_conf, sof_host_ack_write);
37 	u32 dsp_msg, dsp_ack, host_msg, host_ack, irq_stat;
38 
39 	dsp_msg = snd_sof_dsp_read(sdev, ACP_DSP_BAR, ACP_SCRATCH_REG_0 + dsp_msg_write);
40 	dsp_ack = snd_sof_dsp_read(sdev, ACP_DSP_BAR, ACP_SCRATCH_REG_0 + dsp_ack_write);
41 	host_msg = snd_sof_dsp_read(sdev, ACP_DSP_BAR, ACP_SCRATCH_REG_0 + host_msg_write);
42 	host_ack = snd_sof_dsp_read(sdev, ACP_DSP_BAR, ACP_SCRATCH_REG_0 + host_ack_write);
43 	irq_stat = snd_sof_dsp_read(sdev, ACP_DSP_BAR, base + DSP_SW_INTR_STAT_OFFSET);
44 
45 	dev_err(sdev->dev,
46 		"dsp_msg = %#x dsp_ack = %#x host_msg = %#x host_ack = %#x irq_stat = %#x\n",
47 		dsp_msg, dsp_ack, host_msg, host_ack, irq_stat);
48 }
49 
50 /**
51  * amd_get_registers() - This function is called in case of DSP oops
52  * in order to gather information about the registers, filename and
53  * linenumber and stack.
54  * @sdev: SOF device.
55  * @xoops: Stores information about registers.
56  * @panic_info: Stores information about filename and line number.
57  * @stack: Stores the stack dump.
58  * @stack_words: Size of the stack dump.
59  */
60 static void amd_get_registers(struct snd_sof_dev *sdev,
61 			      struct sof_ipc_dsp_oops_xtensa *xoops,
62 			      struct sof_ipc_panic_info *panic_info,
63 			      u32 *stack, size_t stack_words)
64 {
65 	u32 offset = sdev->dsp_oops_offset;
66 
67 	/* first read registers */
68 	acp_mailbox_read(sdev, offset, xoops, sizeof(*xoops));
69 
70 	/* then get panic info */
71 	if (xoops->arch_hdr.totalsize > EXCEPT_MAX_HDR_SIZE) {
72 		dev_err(sdev->dev, "invalid header size 0x%x. FW oops is bogus\n",
73 			xoops->arch_hdr.totalsize);
74 		return;
75 	}
76 
77 	offset += xoops->arch_hdr.totalsize;
78 	acp_mailbox_read(sdev, offset, panic_info, sizeof(*panic_info));
79 
80 	/* then get the stack */
81 	offset += sizeof(*panic_info);
82 	acp_mailbox_read(sdev, offset, stack, stack_words * sizeof(u32));
83 }
84 
85 /**
86  * amd_sof_dump() - This function is called when a panic message is
87  * received from the firmware.
88  * @sdev: SOF device.
89  * @flags: parameter not used but required by ops prototype
90  */
91 void amd_sof_dump(struct snd_sof_dev *sdev, u32 flags)
92 {
93 	struct sof_ipc_dsp_oops_xtensa xoops;
94 	struct sof_ipc_panic_info panic_info;
95 	u32 stack[AMD_STACK_DUMP_SIZE];
96 	u32 status;
97 
98 	/* Get information about the panic status from the debug box area.
99 	 * Compute the trace point based on the status.
100 	 */
101 	if (sdev->dsp_oops_offset > sdev->debug_box.offset) {
102 		acp_mailbox_read(sdev, sdev->debug_box.offset, &status, sizeof(u32));
103 	} else {
104 		/* Read DSP Panic status from dsp_box.
105 		 * As window information for exception box offset and size is not available
106 		 * before FW_READY
107 		 */
108 		acp_mailbox_read(sdev, sdev->dsp_box.offset, &status, sizeof(u32));
109 		sdev->dsp_oops_offset = sdev->dsp_box.offset + sizeof(status);
110 	}
111 
112 	/* Get information about the registers, the filename and line
113 	 * number and the stack.
114 	 */
115 	amd_get_registers(sdev, &xoops, &panic_info, stack, AMD_STACK_DUMP_SIZE);
116 
117 	/* Print the information to the console */
118 	sof_print_oops_and_stack(sdev, KERN_ERR, status, status, &xoops,
119 				 &panic_info, stack, AMD_STACK_DUMP_SIZE);
120 }
121 
122 #if IS_ENABLED(CONFIG_SND_SOC_SOF_AMD_SOUNDWIRE)
123 static int amd_sof_sdw_get_slave_info(struct snd_sof_dev *sdev)
124 {
125 	struct acp_dev_data *acp_data = sdev->pdata->hw_pdata;
126 
127 	return sdw_amd_get_slave_info(acp_data->sdw);
128 }
129 
130 static struct snd_soc_acpi_mach *amd_sof_sdw_machine_select(struct snd_sof_dev *sdev)
131 {
132 	struct snd_soc_acpi_mach *mach;
133 	const struct snd_soc_acpi_link_adr *link;
134 	struct acp_dev_data *acp_data = sdev->pdata->hw_pdata;
135 	struct amd_pdm_pdata *pdm_pdata;
136 	int ret, i;
137 
138 	if (acp_data->info.count) {
139 		ret = amd_sof_sdw_get_slave_info(sdev);
140 		if (ret) {
141 			dev_info(sdev->dev, "failed to read slave information\n");
142 			return NULL;
143 		}
144 		for (mach = sdev->pdata->desc->alt_machines; mach; mach++) {
145 			if (!mach->links)
146 				break;
147 			link = mach->links;
148 			for (i = 0; i < acp_data->info.count && link->num_adr; link++, i++) {
149 				if (!snd_soc_acpi_sdw_link_slaves_found(sdev->dev, link,
150 									acp_data->sdw->peripherals))
151 					break;
152 			}
153 			if (i == acp_data->info.count || !link->num_adr)
154 				if (!mach->machine_check || mach->machine_check(acp_data->sdw))
155 					break;
156 		}
157 		if (mach && mach->link_mask) {
158 			mach->mach_params.subsystem_rev = acp_data->pci_rev;
159 			mach->mach_params.subsystem_vendor = acp_data->subsystem_vendor;
160 			mach->mach_params.subsystem_device = acp_data->subsystem_device;
161 			mach->mach_params.subsystem_id_set = true;
162 			mach->mach_params.links = mach->links;
163 			mach->mach_params.link_mask = mach->link_mask;
164 			mach->mach_params.platform = dev_name(sdev->dev);
165 
166 			if (acp_data->pdm_sel) {
167 				pdm_pdata = devm_kzalloc_obj(sdev->dev,
168 							     *pdm_pdata);
169 				if (!pdm_pdata)
170 					return NULL;
171 				pdm_pdata->pdm_sel = acp_data->pdm_sel;
172 				mach->pdata = pdm_pdata;
173 			}
174 
175 			dev_dbg(sdev->dev, "SSID %x%04x\n", mach->mach_params.subsystem_vendor,
176 				mach->mach_params.subsystem_device);
177 			return mach;
178 		}
179 	}
180 	dev_info(sdev->dev, "No SoundWire machine driver found\n");
181 	return NULL;
182 }
183 
184 #else
185 static struct snd_soc_acpi_mach *amd_sof_sdw_machine_select(struct snd_sof_dev *sdev)
186 {
187 	return NULL;
188 }
189 #endif
190 
191 struct snd_soc_acpi_mach *amd_sof_machine_select(struct snd_sof_dev *sdev)
192 {
193 	struct snd_sof_pdata *sof_pdata = sdev->pdata;
194 	struct acp_dev_data *acp_data = sdev->pdata->hw_pdata;
195 	const struct sof_dev_desc *desc = sof_pdata->desc;
196 	struct snd_soc_acpi_mach *mach = NULL;
197 	struct amd_pdm_pdata *pdm_pdata;
198 
199 	if (desc->machines)
200 		mach = snd_soc_acpi_find_machine(desc->machines);
201 	if (!mach) {
202 		mach = amd_sof_sdw_machine_select(sdev);
203 		if (!mach) {
204 			dev_warn(sdev->dev, "No matching ASoC machine driver found\n");
205 			return NULL;
206 		}
207 	}
208 
209 	mach = devm_kmemdup(sdev->dev, mach, sizeof(*mach), GFP_KERNEL);
210 	if (!mach) {
211 		dev_err(sdev->dev, "failed to allocate machine entry copy\n");
212 		return NULL;
213 	}
214 
215 	mach->mach_params.subsystem_rev = acp_data->pci_rev;
216 
217 	if (acp_data->pdm_sel) {
218 		pdm_pdata = devm_kzalloc_obj(sdev->dev, *pdm_pdata);
219 		if (!pdm_pdata)
220 			return NULL;
221 		pdm_pdata->pdm_sel = acp_data->pdm_sel;
222 		mach->pdata = pdm_pdata;
223 	}
224 
225 	sof_pdata->tplg_filename = mach->sof_tplg_filename;
226 	sof_pdata->fw_filename = mach->fw_filename;
227 
228 	return mach;
229 }
230 
231 /* AMD Common DSP ops */
232 const struct snd_sof_dsp_ops sof_acp_common_ops = {
233 	/* probe and remove */
234 	.probe			= amd_sof_acp_probe,
235 	.remove			= amd_sof_acp_remove,
236 
237 	/* Register IO */
238 	.write			= sof_io_write,
239 	.read			= sof_io_read,
240 
241 	/* Block IO */
242 	.block_read		= acp_dsp_block_read,
243 	.block_write		= acp_dsp_block_write,
244 
245 	/*Firmware loading */
246 	.load_firmware		= snd_sof_load_firmware_memcpy,
247 	.pre_fw_run		= acp_dsp_pre_fw_run,
248 	.get_bar_index		= acp_get_bar_index,
249 
250 	/* DSP core boot */
251 	.run			= acp_sof_dsp_run,
252 
253 	/*IPC */
254 	.send_msg		= acp_sof_ipc_send_msg,
255 	.ipc_msg_data		= acp_sof_ipc_msg_data,
256 	.set_stream_data_offset = acp_set_stream_data_offset,
257 	.get_mailbox_offset	= acp_sof_ipc_get_mailbox_offset,
258 	.get_window_offset      = acp_sof_ipc_get_window_offset,
259 	.irq_thread		= acp_sof_ipc_irq_thread,
260 
261 	/* stream callbacks */
262 	.pcm_open		= acp_pcm_open,
263 	.pcm_close		= acp_pcm_close,
264 	.pcm_hw_params		= acp_pcm_hw_params,
265 	.pcm_pointer		= acp_pcm_pointer,
266 
267 	.hw_info		= SNDRV_PCM_INFO_MMAP |
268 				  SNDRV_PCM_INFO_MMAP_VALID |
269 				  SNDRV_PCM_INFO_INTERLEAVED |
270 				  SNDRV_PCM_INFO_PAUSE |
271 				  SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
272 
273 	/* Machine driver callbacks */
274 	.machine_select		= amd_sof_machine_select,
275 	.machine_register	= sof_machine_register,
276 	.machine_unregister	= sof_machine_unregister,
277 
278 	/* Trace Logger */
279 	.trace_init		= acp_sof_trace_init,
280 	.trace_release		= acp_sof_trace_release,
281 
282 	/* PM */
283 	.suspend                = amd_sof_acp_suspend,
284 	.resume                 = amd_sof_acp_resume,
285 
286 	.ipc_dump		= amd_sof_ipc_dump,
287 	.dbg_dump		= amd_sof_dump,
288 	.debugfs_add_region_item = snd_sof_debugfs_add_region_item_iomem,
289 	.dsp_arch_ops = &sof_xtensa_arch_ops,
290 
291 	/* probe client device registation */
292 	.register_ipc_clients = acp_probes_register,
293 	.unregister_ipc_clients = acp_probes_unregister,
294 };
295 EXPORT_SYMBOL_NS(sof_acp_common_ops, "SND_SOC_SOF_AMD_COMMON");
296 
297 MODULE_LICENSE("Dual BSD/GPL");
298 MODULE_DESCRIPTION("ACP SOF COMMON Driver");
299 MODULE_IMPORT_NS("SND_SOC_SOF_AMD_COMMON");
300 MODULE_IMPORT_NS("SND_SOC_SOF_XTENSA");
301 MODULE_IMPORT_NS("SOUNDWIRE_AMD_INIT");
302