xref: /openbmc/linux/sound/soc/sof/intel/atom.c (revision d9679d00)
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-2021 Intel Corporation. All rights reserved.
7 //
8 // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9 //
10 
11 /*
12  * Hardware interface for audio DSP on Atom devices
13  */
14 
15 #include <linux/module.h>
16 #include <sound/sof.h>
17 #include <sound/sof/xtensa.h>
18 #include <sound/soc-acpi.h>
19 #include <sound/soc-acpi-intel-match.h>
20 #include <sound/intel-dsp-config.h>
21 #include "../ops.h"
22 #include "shim.h"
23 #include "atom.h"
24 #include "../sof-acpi-dev.h"
25 #include "../sof-audio.h"
26 #include "../../intel/common/soc-intel-quirks.h"
27 
28 static void atom_host_done(struct snd_sof_dev *sdev);
29 static void atom_dsp_done(struct snd_sof_dev *sdev);
30 static void atom_get_reply(struct snd_sof_dev *sdev);
31 
32 /*
33  * Debug
34  */
35 
36 static void atom_get_registers(struct snd_sof_dev *sdev,
37 			       struct sof_ipc_dsp_oops_xtensa *xoops,
38 			       struct sof_ipc_panic_info *panic_info,
39 			       u32 *stack, size_t stack_words)
40 {
41 	u32 offset = sdev->dsp_oops_offset;
42 
43 	/* first read regsisters */
44 	sof_mailbox_read(sdev, offset, xoops, sizeof(*xoops));
45 
46 	/* note: variable AR register array is not read */
47 
48 	/* then get panic info */
49 	if (xoops->arch_hdr.totalsize > EXCEPT_MAX_HDR_SIZE) {
50 		dev_err(sdev->dev, "invalid header size 0x%x. FW oops is bogus\n",
51 			xoops->arch_hdr.totalsize);
52 		return;
53 	}
54 	offset += xoops->arch_hdr.totalsize;
55 	sof_mailbox_read(sdev, offset, panic_info, sizeof(*panic_info));
56 
57 	/* then get the stack */
58 	offset += sizeof(*panic_info);
59 	sof_mailbox_read(sdev, offset, stack, stack_words * sizeof(u32));
60 }
61 
62 void atom_dump(struct snd_sof_dev *sdev, u32 flags)
63 {
64 	struct sof_ipc_dsp_oops_xtensa xoops;
65 	struct sof_ipc_panic_info panic_info;
66 	u32 stack[STACK_DUMP_SIZE];
67 	u64 status, panic, imrd, imrx;
68 
69 	/* now try generic SOF status messages */
70 	status = snd_sof_dsp_read64(sdev, DSP_BAR, SHIM_IPCD);
71 	panic = snd_sof_dsp_read64(sdev, DSP_BAR, SHIM_IPCX);
72 	atom_get_registers(sdev, &xoops, &panic_info, stack,
73 			   STACK_DUMP_SIZE);
74 	snd_sof_get_status(sdev, status, panic, &xoops, &panic_info, stack,
75 			   STACK_DUMP_SIZE);
76 
77 	/* provide some context for firmware debug */
78 	imrx = snd_sof_dsp_read64(sdev, DSP_BAR, SHIM_IMRX);
79 	imrd = snd_sof_dsp_read64(sdev, DSP_BAR, SHIM_IMRD);
80 	dev_err(sdev->dev,
81 		"error: ipc host -> DSP: pending %s complete %s raw 0x%llx\n",
82 		(panic & SHIM_IPCX_BUSY) ? "yes" : "no",
83 		(panic & SHIM_IPCX_DONE) ? "yes" : "no", panic);
84 	dev_err(sdev->dev,
85 		"error: mask host: pending %s complete %s raw 0x%llx\n",
86 		(imrx & SHIM_IMRX_BUSY) ? "yes" : "no",
87 		(imrx & SHIM_IMRX_DONE) ? "yes" : "no", imrx);
88 	dev_err(sdev->dev,
89 		"error: ipc DSP -> host: pending %s complete %s raw 0x%llx\n",
90 		(status & SHIM_IPCD_BUSY) ? "yes" : "no",
91 		(status & SHIM_IPCD_DONE) ? "yes" : "no", status);
92 	dev_err(sdev->dev,
93 		"error: mask DSP: pending %s complete %s raw 0x%llx\n",
94 		(imrd & SHIM_IMRD_BUSY) ? "yes" : "no",
95 		(imrd & SHIM_IMRD_DONE) ? "yes" : "no", imrd);
96 
97 }
98 EXPORT_SYMBOL_NS(atom_dump, SND_SOC_SOF_INTEL_ATOM_HIFI_EP);
99 
100 /*
101  * IPC Doorbell IRQ handler and thread.
102  */
103 
104 irqreturn_t atom_irq_handler(int irq, void *context)
105 {
106 	struct snd_sof_dev *sdev = context;
107 	u64 ipcx, ipcd;
108 	int ret = IRQ_NONE;
109 
110 	ipcx = snd_sof_dsp_read64(sdev, DSP_BAR, SHIM_IPCX);
111 	ipcd = snd_sof_dsp_read64(sdev, DSP_BAR, SHIM_IPCD);
112 
113 	if (ipcx & SHIM_BYT_IPCX_DONE) {
114 
115 		/* reply message from DSP, Mask Done interrupt first */
116 		snd_sof_dsp_update_bits64_unlocked(sdev, DSP_BAR,
117 						   SHIM_IMRX,
118 						   SHIM_IMRX_DONE,
119 						   SHIM_IMRX_DONE);
120 		ret = IRQ_WAKE_THREAD;
121 	}
122 
123 	if (ipcd & SHIM_BYT_IPCD_BUSY) {
124 
125 		/* new message from DSP, Mask Busy interrupt first */
126 		snd_sof_dsp_update_bits64_unlocked(sdev, DSP_BAR,
127 						   SHIM_IMRX,
128 						   SHIM_IMRX_BUSY,
129 						   SHIM_IMRX_BUSY);
130 		ret = IRQ_WAKE_THREAD;
131 	}
132 
133 	return ret;
134 }
135 EXPORT_SYMBOL_NS(atom_irq_handler, SND_SOC_SOF_INTEL_ATOM_HIFI_EP);
136 
137 irqreturn_t atom_irq_thread(int irq, void *context)
138 {
139 	struct snd_sof_dev *sdev = context;
140 	u64 ipcx, ipcd;
141 
142 	ipcx = snd_sof_dsp_read64(sdev, DSP_BAR, SHIM_IPCX);
143 	ipcd = snd_sof_dsp_read64(sdev, DSP_BAR, SHIM_IPCD);
144 
145 	/* reply message from DSP */
146 	if (ipcx & SHIM_BYT_IPCX_DONE) {
147 
148 		spin_lock_irq(&sdev->ipc_lock);
149 
150 		/*
151 		 * handle immediate reply from DSP core. If the msg is
152 		 * found, set done bit in cmd_done which is called at the
153 		 * end of message processing function, else set it here
154 		 * because the done bit can't be set in cmd_done function
155 		 * which is triggered by msg
156 		 */
157 		atom_get_reply(sdev);
158 		snd_sof_ipc_reply(sdev, ipcx);
159 
160 		atom_dsp_done(sdev);
161 
162 		spin_unlock_irq(&sdev->ipc_lock);
163 	}
164 
165 	/* new message from DSP */
166 	if (ipcd & SHIM_BYT_IPCD_BUSY) {
167 
168 		/* Handle messages from DSP Core */
169 		if ((ipcd & SOF_IPC_PANIC_MAGIC_MASK) == SOF_IPC_PANIC_MAGIC) {
170 			snd_sof_dsp_panic(sdev, PANIC_OFFSET(ipcd) +
171 					  MBOX_OFFSET);
172 		} else {
173 			snd_sof_ipc_msgs_rx(sdev);
174 		}
175 
176 		atom_host_done(sdev);
177 	}
178 
179 	return IRQ_HANDLED;
180 }
181 EXPORT_SYMBOL_NS(atom_irq_thread, SND_SOC_SOF_INTEL_ATOM_HIFI_EP);
182 
183 int atom_send_msg(struct snd_sof_dev *sdev, struct snd_sof_ipc_msg *msg)
184 {
185 	/* unmask and prepare to receive Done interrupt */
186 	snd_sof_dsp_update_bits64_unlocked(sdev, DSP_BAR, SHIM_IMRX,
187 					   SHIM_IMRX_DONE, 0);
188 
189 	/* send the message */
190 	sof_mailbox_write(sdev, sdev->host_box.offset, msg->msg_data,
191 			  msg->msg_size);
192 	snd_sof_dsp_write64(sdev, DSP_BAR, SHIM_IPCX, SHIM_BYT_IPCX_BUSY);
193 
194 	return 0;
195 }
196 EXPORT_SYMBOL_NS(atom_send_msg, SND_SOC_SOF_INTEL_ATOM_HIFI_EP);
197 
198 static void atom_get_reply(struct snd_sof_dev *sdev)
199 {
200 	struct snd_sof_ipc_msg *msg = sdev->msg;
201 	struct sof_ipc_reply reply;
202 	int ret = 0;
203 
204 	/*
205 	 * Sometimes, there is unexpected reply ipc arriving. The reply
206 	 * ipc belongs to none of the ipcs sent from driver.
207 	 * In this case, the driver must ignore the ipc.
208 	 */
209 	if (!msg) {
210 		dev_warn(sdev->dev, "unexpected ipc interrupt raised!\n");
211 		return;
212 	}
213 
214 	/* get reply */
215 	sof_mailbox_read(sdev, sdev->host_box.offset, &reply, sizeof(reply));
216 
217 	if (reply.error < 0) {
218 		memcpy(msg->reply_data, &reply, sizeof(reply));
219 		ret = reply.error;
220 	} else {
221 		/* reply correct size ? */
222 		if (reply.hdr.size != msg->reply_size) {
223 			dev_err(sdev->dev, "error: reply expected %zu got %u bytes\n",
224 				msg->reply_size, reply.hdr.size);
225 			ret = -EINVAL;
226 		}
227 
228 		/* read the message */
229 		if (msg->reply_size > 0)
230 			sof_mailbox_read(sdev, sdev->host_box.offset,
231 					 msg->reply_data, msg->reply_size);
232 	}
233 
234 	msg->reply_error = ret;
235 }
236 
237 int atom_get_mailbox_offset(struct snd_sof_dev *sdev)
238 {
239 	return MBOX_OFFSET;
240 }
241 EXPORT_SYMBOL_NS(atom_get_mailbox_offset, SND_SOC_SOF_INTEL_ATOM_HIFI_EP);
242 
243 int atom_get_window_offset(struct snd_sof_dev *sdev, u32 id)
244 {
245 	return MBOX_OFFSET;
246 }
247 EXPORT_SYMBOL_NS(atom_get_window_offset, SND_SOC_SOF_INTEL_ATOM_HIFI_EP);
248 
249 static void atom_host_done(struct snd_sof_dev *sdev)
250 {
251 	/* clear BUSY bit and set DONE bit - accept new messages */
252 	snd_sof_dsp_update_bits64_unlocked(sdev, DSP_BAR, SHIM_IPCD,
253 					   SHIM_BYT_IPCD_BUSY |
254 					   SHIM_BYT_IPCD_DONE,
255 					   SHIM_BYT_IPCD_DONE);
256 
257 	/* unmask and prepare to receive next new message */
258 	snd_sof_dsp_update_bits64_unlocked(sdev, DSP_BAR, SHIM_IMRX,
259 					   SHIM_IMRX_BUSY, 0);
260 }
261 
262 static void atom_dsp_done(struct snd_sof_dev *sdev)
263 {
264 	/* clear DONE bit - tell DSP we have completed */
265 	snd_sof_dsp_update_bits64_unlocked(sdev, DSP_BAR, SHIM_IPCX,
266 					   SHIM_BYT_IPCX_DONE, 0);
267 }
268 
269 /*
270  * DSP control.
271  */
272 
273 int atom_run(struct snd_sof_dev *sdev)
274 {
275 	int tries = 10;
276 
277 	/* release stall and wait to unstall */
278 	snd_sof_dsp_update_bits64(sdev, DSP_BAR, SHIM_CSR,
279 				  SHIM_BYT_CSR_STALL, 0x0);
280 	while (tries--) {
281 		if (!(snd_sof_dsp_read64(sdev, DSP_BAR, SHIM_CSR) &
282 		      SHIM_BYT_CSR_PWAITMODE))
283 			break;
284 		msleep(100);
285 	}
286 	if (tries < 0)
287 		return -ENODEV;
288 
289 	/* return init core mask */
290 	return 1;
291 }
292 EXPORT_SYMBOL_NS(atom_run, SND_SOC_SOF_INTEL_ATOM_HIFI_EP);
293 
294 int atom_reset(struct snd_sof_dev *sdev)
295 {
296 	/* put DSP into reset, set reset vector and stall */
297 	snd_sof_dsp_update_bits64(sdev, DSP_BAR, SHIM_CSR,
298 				  SHIM_BYT_CSR_RST | SHIM_BYT_CSR_VECTOR_SEL |
299 				  SHIM_BYT_CSR_STALL,
300 				  SHIM_BYT_CSR_RST | SHIM_BYT_CSR_VECTOR_SEL |
301 				  SHIM_BYT_CSR_STALL);
302 
303 	usleep_range(10, 15);
304 
305 	/* take DSP out of reset and keep stalled for FW loading */
306 	snd_sof_dsp_update_bits64(sdev, DSP_BAR, SHIM_CSR,
307 				  SHIM_BYT_CSR_RST, 0);
308 
309 	return 0;
310 }
311 EXPORT_SYMBOL_NS(atom_reset, SND_SOC_SOF_INTEL_ATOM_HIFI_EP);
312 
313 static const char *fixup_tplg_name(struct snd_sof_dev *sdev,
314 				   const char *sof_tplg_filename,
315 				   const char *ssp_str)
316 {
317 	const char *tplg_filename = NULL;
318 	char *filename;
319 	char *split_ext;
320 
321 	filename = devm_kstrdup(sdev->dev, sof_tplg_filename, GFP_KERNEL);
322 	if (!filename)
323 		return NULL;
324 
325 	/* this assumes a .tplg extension */
326 	split_ext = strsep(&filename, ".");
327 	if (split_ext) {
328 		tplg_filename = devm_kasprintf(sdev->dev, GFP_KERNEL,
329 					       "%s-%s.tplg",
330 					       split_ext, ssp_str);
331 		if (!tplg_filename)
332 			return NULL;
333 	}
334 	return tplg_filename;
335 }
336 
337 void atom_machine_select(struct snd_sof_dev *sdev)
338 {
339 	struct snd_sof_pdata *sof_pdata = sdev->pdata;
340 	const struct sof_dev_desc *desc = sof_pdata->desc;
341 	struct snd_soc_acpi_mach *mach;
342 	struct platform_device *pdev;
343 	const char *tplg_filename;
344 
345 	mach = snd_soc_acpi_find_machine(desc->machines);
346 	if (!mach) {
347 		dev_warn(sdev->dev, "warning: No matching ASoC machine driver found\n");
348 		return;
349 	}
350 
351 	pdev = to_platform_device(sdev->dev);
352 	if (soc_intel_is_byt_cr(pdev)) {
353 		dev_dbg(sdev->dev,
354 			"BYT-CR detected, SSP0 used instead of SSP2\n");
355 
356 		tplg_filename = fixup_tplg_name(sdev,
357 						mach->sof_tplg_filename,
358 						"ssp0");
359 	} else {
360 		tplg_filename = mach->sof_tplg_filename;
361 	}
362 
363 	if (!tplg_filename) {
364 		dev_dbg(sdev->dev,
365 			"error: no topology filename\n");
366 		return;
367 	}
368 
369 	sof_pdata->tplg_filename = tplg_filename;
370 	mach->mach_params.acpi_ipc_irq_index = desc->irqindex_host_ipc;
371 	sof_pdata->machine = mach;
372 }
373 EXPORT_SYMBOL_NS(atom_machine_select, SND_SOC_SOF_INTEL_ATOM_HIFI_EP);
374 
375 /* Atom DAIs */
376 struct snd_soc_dai_driver atom_dai[] = {
377 {
378 	.name = "ssp0-port",
379 	.playback = {
380 		.channels_min = 1,
381 		.channels_max = 8,
382 	},
383 	.capture = {
384 		.channels_min = 1,
385 		.channels_max = 8,
386 	},
387 },
388 {
389 	.name = "ssp1-port",
390 	.playback = {
391 		.channels_min = 1,
392 		.channels_max = 8,
393 	},
394 	.capture = {
395 		.channels_min = 1,
396 		.channels_max = 8,
397 	},
398 },
399 {
400 	.name = "ssp2-port",
401 	.playback = {
402 		.channels_min = 1,
403 		.channels_max = 8,
404 	},
405 	.capture = {
406 		.channels_min = 1,
407 		.channels_max = 8,
408 	}
409 },
410 {
411 	.name = "ssp3-port",
412 	.playback = {
413 		.channels_min = 1,
414 		.channels_max = 8,
415 	},
416 	.capture = {
417 		.channels_min = 1,
418 		.channels_max = 8,
419 	},
420 },
421 {
422 	.name = "ssp4-port",
423 	.playback = {
424 		.channels_min = 1,
425 		.channels_max = 8,
426 	},
427 	.capture = {
428 		.channels_min = 1,
429 		.channels_max = 8,
430 	},
431 },
432 {
433 	.name = "ssp5-port",
434 	.playback = {
435 		.channels_min = 1,
436 		.channels_max = 8,
437 	},
438 	.capture = {
439 		.channels_min = 1,
440 		.channels_max = 8,
441 	},
442 },
443 };
444 EXPORT_SYMBOL_NS(atom_dai, SND_SOC_SOF_INTEL_ATOM_HIFI_EP);
445 
446 void atom_set_mach_params(const struct snd_soc_acpi_mach *mach,
447 			  struct snd_sof_dev *sdev)
448 {
449 	struct snd_sof_pdata *pdata = sdev->pdata;
450 	const struct sof_dev_desc *desc = pdata->desc;
451 	struct snd_soc_acpi_mach_params *mach_params;
452 
453 	mach_params = (struct snd_soc_acpi_mach_params *)&mach->mach_params;
454 	mach_params->platform = dev_name(sdev->dev);
455 	mach_params->num_dai_drivers = desc->ops->num_drv;
456 	mach_params->dai_drivers = desc->ops->drv;
457 }
458 EXPORT_SYMBOL_NS(atom_set_mach_params, SND_SOC_SOF_INTEL_ATOM_HIFI_EP);
459 
460 MODULE_LICENSE("Dual BSD/GPL");
461