xref: /linux/sound/soc/sof/amd/acp.c (revision eb01fe7abbe2d0b38824d2a93fdb4cc3eaf2ccc1)
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) 2021, 2023 Advanced Micro Devices, Inc. All rights reserved.
7 //
8 // Authors: Vijendar Mukunda <Vijendar.Mukunda@amd.com>
9 //	    Ajit Kumar Pandey <AjitKumar.Pandey@amd.com>
10 
11 /*
12  * Hardware interface for generic AMD ACP processor
13  */
14 
15 #include <linux/io.h>
16 #include <linux/module.h>
17 #include <linux/pci.h>
18 
19 #include "../ops.h"
20 #include "acp.h"
21 #include "acp-dsp-offset.h"
22 
23 #define SECURED_FIRMWARE 1
24 
25 static bool enable_fw_debug;
26 module_param(enable_fw_debug, bool, 0444);
27 MODULE_PARM_DESC(enable_fw_debug, "Enable Firmware debug");
28 
29 const struct dmi_system_id acp_sof_quirk_table[] = {
30 	{
31 		/* Steam Deck OLED device */
32 		.matches = {
33 			DMI_MATCH(DMI_SYS_VENDOR, "Valve"),
34 			DMI_MATCH(DMI_PRODUCT_NAME, "Galileo"),
35 		},
36 		.driver_data = (void *)SECURED_FIRMWARE,
37 	},
38 	{}
39 };
40 EXPORT_SYMBOL_GPL(acp_sof_quirk_table);
41 
42 static int smn_write(struct pci_dev *dev, u32 smn_addr, u32 data)
43 {
44 	pci_write_config_dword(dev, 0x60, smn_addr);
45 	pci_write_config_dword(dev, 0x64, data);
46 
47 	return 0;
48 }
49 
50 static int smn_read(struct pci_dev *dev, u32 smn_addr)
51 {
52 	u32 data = 0;
53 
54 	pci_write_config_dword(dev, 0x60, smn_addr);
55 	pci_read_config_dword(dev, 0x64, &data);
56 
57 	return data;
58 }
59 
60 static void init_dma_descriptor(struct acp_dev_data *adata)
61 {
62 	struct snd_sof_dev *sdev = adata->dev;
63 	const struct sof_amd_acp_desc *desc = get_chip_info(sdev->pdata);
64 	unsigned int addr;
65 
66 	addr = desc->sram_pte_offset + sdev->debug_box.offset +
67 	       offsetof(struct scratch_reg_conf, dma_desc);
68 
69 	snd_sof_dsp_write(sdev, ACP_DSP_BAR, ACP_DMA_DESC_BASE_ADDR, addr);
70 	snd_sof_dsp_write(sdev, ACP_DSP_BAR, ACP_DMA_DESC_MAX_NUM_DSCR, ACP_MAX_DESC_CNT);
71 }
72 
73 static void configure_dma_descriptor(struct acp_dev_data *adata, unsigned short idx,
74 				     struct dma_descriptor *dscr_info)
75 {
76 	struct snd_sof_dev *sdev = adata->dev;
77 	unsigned int offset;
78 
79 	offset = ACP_SCRATCH_REG_0 + sdev->debug_box.offset +
80 		offsetof(struct scratch_reg_conf, dma_desc) +
81 		idx * sizeof(struct dma_descriptor);
82 
83 	snd_sof_dsp_write(sdev, ACP_DSP_BAR, offset, dscr_info->src_addr);
84 	snd_sof_dsp_write(sdev, ACP_DSP_BAR, offset + 0x4, dscr_info->dest_addr);
85 	snd_sof_dsp_write(sdev, ACP_DSP_BAR, offset + 0x8, dscr_info->tx_cnt.u32_all);
86 }
87 
88 static int config_dma_channel(struct acp_dev_data *adata, unsigned int ch,
89 			      unsigned int idx, unsigned int dscr_count)
90 {
91 	struct snd_sof_dev *sdev = adata->dev;
92 	unsigned int val, status;
93 	int ret;
94 
95 	snd_sof_dsp_write(sdev, ACP_DSP_BAR, ACP_DMA_CNTL_0 + ch * sizeof(u32),
96 			  ACP_DMA_CH_RST | ACP_DMA_CH_GRACEFUL_RST_EN);
97 
98 	ret = snd_sof_dsp_read_poll_timeout(sdev, ACP_DSP_BAR, ACP_DMA_CH_RST_STS, val,
99 					    val & (1 << ch), ACP_REG_POLL_INTERVAL,
100 					    ACP_REG_POLL_TIMEOUT_US);
101 	if (ret < 0) {
102 		status = snd_sof_dsp_read(sdev, ACP_DSP_BAR, ACP_ERROR_STATUS);
103 		val = snd_sof_dsp_read(sdev, ACP_DSP_BAR, ACP_DMA_ERR_STS_0 + ch * sizeof(u32));
104 
105 		dev_err(sdev->dev, "ACP_DMA_ERR_STS :0x%x ACP_ERROR_STATUS :0x%x\n", val, status);
106 		return ret;
107 	}
108 
109 	snd_sof_dsp_write(sdev, ACP_DSP_BAR, (ACP_DMA_CNTL_0 + ch * sizeof(u32)), 0);
110 	snd_sof_dsp_write(sdev, ACP_DSP_BAR, ACP_DMA_DSCR_CNT_0 + ch * sizeof(u32), dscr_count);
111 	snd_sof_dsp_write(sdev, ACP_DSP_BAR, ACP_DMA_DSCR_STRT_IDX_0 + ch * sizeof(u32), idx);
112 	snd_sof_dsp_write(sdev, ACP_DSP_BAR, ACP_DMA_PRIO_0 + ch * sizeof(u32), 0);
113 	snd_sof_dsp_write(sdev, ACP_DSP_BAR, ACP_DMA_CNTL_0 + ch * sizeof(u32), ACP_DMA_CH_RUN);
114 
115 	return ret;
116 }
117 
118 static int acpbus_dma_start(struct acp_dev_data *adata, unsigned int ch,
119 			    unsigned int dscr_count, struct dma_descriptor *dscr_info)
120 {
121 	struct snd_sof_dev *sdev = adata->dev;
122 	int ret;
123 	u16 dscr;
124 
125 	if (!dscr_info || !dscr_count)
126 		return -EINVAL;
127 
128 	for (dscr = 0; dscr < dscr_count; dscr++)
129 		configure_dma_descriptor(adata, dscr, dscr_info++);
130 
131 	ret = config_dma_channel(adata, ch, 0, dscr_count);
132 	if (ret < 0)
133 		dev_err(sdev->dev, "config dma ch failed:%d\n", ret);
134 
135 	return ret;
136 }
137 
138 int configure_and_run_dma(struct acp_dev_data *adata, unsigned int src_addr,
139 			  unsigned int dest_addr, int dsp_data_size)
140 {
141 	struct snd_sof_dev *sdev = adata->dev;
142 	unsigned int desc_count, index;
143 	int ret;
144 
145 	for (desc_count = 0; desc_count < ACP_MAX_DESC && dsp_data_size >= 0;
146 	     desc_count++, dsp_data_size -= ACP_PAGE_SIZE) {
147 		adata->dscr_info[desc_count].src_addr = src_addr + desc_count * ACP_PAGE_SIZE;
148 		adata->dscr_info[desc_count].dest_addr = dest_addr + desc_count * ACP_PAGE_SIZE;
149 		adata->dscr_info[desc_count].tx_cnt.bits.count = ACP_PAGE_SIZE;
150 		if (dsp_data_size < ACP_PAGE_SIZE)
151 			adata->dscr_info[desc_count].tx_cnt.bits.count = dsp_data_size;
152 	}
153 
154 	ret = acpbus_dma_start(adata, 0, desc_count, adata->dscr_info);
155 	if (ret)
156 		dev_err(sdev->dev, "acpbus_dma_start failed\n");
157 
158 	/* Clear descriptor array */
159 	for (index = 0; index < desc_count; index++)
160 		memset(&adata->dscr_info[index], 0x00, sizeof(struct dma_descriptor));
161 
162 	return ret;
163 }
164 
165 /*
166  * psp_mbox_ready- function to poll ready bit of psp mbox
167  * @adata: acp device data
168  * @ack: bool variable to check ready bit status or psp ack
169  */
170 
171 static int psp_mbox_ready(struct acp_dev_data *adata, bool ack)
172 {
173 	struct snd_sof_dev *sdev = adata->dev;
174 	int ret;
175 	u32 data;
176 
177 	ret = read_poll_timeout(smn_read, data, data & MBOX_READY_MASK, MBOX_DELAY_US,
178 				ACP_PSP_TIMEOUT_US, false, adata->smn_dev, MP0_C2PMSG_114_REG);
179 	if (!ret)
180 		return 0;
181 
182 	dev_err(sdev->dev, "PSP error status %x\n", data & MBOX_STATUS_MASK);
183 
184 	if (ack)
185 		return -ETIMEDOUT;
186 
187 	return -EBUSY;
188 }
189 
190 /*
191  * psp_send_cmd - function to send psp command over mbox
192  * @adata: acp device data
193  * @cmd: non zero integer value for command type
194  */
195 
196 static int psp_send_cmd(struct acp_dev_data *adata, int cmd)
197 {
198 	struct snd_sof_dev *sdev = adata->dev;
199 	int ret;
200 	u32 data;
201 
202 	if (!cmd)
203 		return -EINVAL;
204 
205 	/* Get a non-zero Doorbell value from PSP */
206 	ret = read_poll_timeout(smn_read, data, data, MBOX_DELAY_US, ACP_PSP_TIMEOUT_US, false,
207 				adata->smn_dev, MP0_C2PMSG_73_REG);
208 
209 	if (ret) {
210 		dev_err(sdev->dev, "Failed to get Doorbell from MBOX %x\n", MP0_C2PMSG_73_REG);
211 		return ret;
212 	}
213 
214 	/* Check if PSP is ready for new command */
215 	ret = psp_mbox_ready(adata, 0);
216 	if (ret)
217 		return ret;
218 
219 	smn_write(adata->smn_dev, MP0_C2PMSG_114_REG, cmd);
220 
221 	/* Ring the Doorbell for PSP */
222 	smn_write(adata->smn_dev, MP0_C2PMSG_73_REG, data);
223 
224 	/* Check MBOX ready as PSP ack */
225 	ret = psp_mbox_ready(adata, 1);
226 
227 	return ret;
228 }
229 
230 int configure_and_run_sha_dma(struct acp_dev_data *adata, void *image_addr,
231 			      unsigned int start_addr, unsigned int dest_addr,
232 			      unsigned int image_length)
233 {
234 	struct snd_sof_dev *sdev = adata->dev;
235 	const struct sof_amd_acp_desc *desc = get_chip_info(sdev->pdata);
236 	unsigned int tx_count, fw_qualifier, val;
237 	int ret;
238 
239 	if (!image_addr) {
240 		dev_err(sdev->dev, "SHA DMA image address is NULL\n");
241 		return -EINVAL;
242 	}
243 
244 	val = snd_sof_dsp_read(sdev, ACP_DSP_BAR, ACP_SHA_DMA_CMD);
245 	if (val & ACP_SHA_RUN) {
246 		snd_sof_dsp_write(sdev, ACP_DSP_BAR, ACP_SHA_DMA_CMD, ACP_SHA_RESET);
247 		ret = snd_sof_dsp_read_poll_timeout(sdev, ACP_DSP_BAR, ACP_SHA_DMA_CMD_STS,
248 						    val, val & ACP_SHA_RESET,
249 						    ACP_REG_POLL_INTERVAL,
250 						    ACP_REG_POLL_TIMEOUT_US);
251 		if (ret < 0) {
252 			dev_err(sdev->dev, "SHA DMA Failed to Reset\n");
253 			return ret;
254 		}
255 	}
256 
257 	if (adata->signed_fw_image)
258 		snd_sof_dsp_write(sdev, ACP_DSP_BAR, ACP_SHA_DMA_INCLUDE_HDR, ACP_SHA_HEADER);
259 
260 	snd_sof_dsp_write(sdev, ACP_DSP_BAR, ACP_SHA_DMA_STRT_ADDR, start_addr);
261 	snd_sof_dsp_write(sdev, ACP_DSP_BAR, ACP_SHA_DMA_DESTINATION_ADDR, dest_addr);
262 	snd_sof_dsp_write(sdev, ACP_DSP_BAR, ACP_SHA_MSG_LENGTH, image_length);
263 	snd_sof_dsp_write(sdev, ACP_DSP_BAR, ACP_SHA_DMA_CMD, ACP_SHA_RUN);
264 
265 	ret = snd_sof_dsp_read_poll_timeout(sdev, ACP_DSP_BAR, ACP_SHA_TRANSFER_BYTE_CNT,
266 					    tx_count, tx_count == image_length,
267 					    ACP_REG_POLL_INTERVAL, ACP_DMA_COMPLETE_TIMEOUT_US);
268 	if (ret < 0) {
269 		dev_err(sdev->dev, "SHA DMA Failed to Transfer Length %x\n", tx_count);
270 		return ret;
271 	}
272 
273 	/* psp_send_cmd only required for renoir platform (rev - 3) */
274 	if (desc->rev == 3) {
275 		ret = psp_send_cmd(adata, MBOX_ACP_SHA_DMA_COMMAND);
276 		if (ret)
277 			return ret;
278 	}
279 
280 	/* psp_send_cmd only required for vangogh platform (rev - 5) */
281 	if (desc->rev == 5) {
282 		/* Modify IRAM and DRAM size */
283 		ret = psp_send_cmd(adata, MBOX_ACP_IRAM_DRAM_FENCE_COMMAND | IRAM_DRAM_FENCE_2);
284 		if (ret)
285 			return ret;
286 		ret = psp_send_cmd(adata, MBOX_ACP_IRAM_DRAM_FENCE_COMMAND | MBOX_ISREADY_FLAG);
287 		if (ret)
288 			return ret;
289 	}
290 
291 	ret = snd_sof_dsp_read_poll_timeout(sdev, ACP_DSP_BAR, ACP_SHA_DSP_FW_QUALIFIER,
292 					    fw_qualifier, fw_qualifier & DSP_FW_RUN_ENABLE,
293 					    ACP_REG_POLL_INTERVAL, ACP_DMA_COMPLETE_TIMEOUT_US);
294 	if (ret < 0) {
295 		dev_err(sdev->dev, "PSP validation failed\n");
296 		return ret;
297 	}
298 
299 	return 0;
300 }
301 
302 int acp_dma_status(struct acp_dev_data *adata, unsigned char ch)
303 {
304 	struct snd_sof_dev *sdev = adata->dev;
305 	unsigned int val;
306 	int ret = 0;
307 
308 	val = snd_sof_dsp_read(sdev, ACP_DSP_BAR, ACP_DMA_CNTL_0 + ch * sizeof(u32));
309 	if (val & ACP_DMA_CH_RUN) {
310 		ret = snd_sof_dsp_read_poll_timeout(sdev, ACP_DSP_BAR, ACP_DMA_CH_STS, val, !val,
311 						    ACP_REG_POLL_INTERVAL,
312 						    ACP_DMA_COMPLETE_TIMEOUT_US);
313 		if (ret < 0)
314 			dev_err(sdev->dev, "DMA_CHANNEL %d status timeout\n", ch);
315 	}
316 
317 	return ret;
318 }
319 
320 void memcpy_from_scratch(struct snd_sof_dev *sdev, u32 offset, unsigned int *dst, size_t bytes)
321 {
322 	unsigned int reg_offset = offset + ACP_SCRATCH_REG_0;
323 	int i, j;
324 
325 	for (i = 0, j = 0; i < bytes; i = i + 4, j++)
326 		dst[j] = snd_sof_dsp_read(sdev, ACP_DSP_BAR, reg_offset + i);
327 }
328 
329 void memcpy_to_scratch(struct snd_sof_dev *sdev, u32 offset, unsigned int *src, size_t bytes)
330 {
331 	unsigned int reg_offset = offset + ACP_SCRATCH_REG_0;
332 	int i, j;
333 
334 	for (i = 0, j = 0; i < bytes; i = i + 4, j++)
335 		snd_sof_dsp_write(sdev, ACP_DSP_BAR, reg_offset + i, src[j]);
336 }
337 
338 static int acp_memory_init(struct snd_sof_dev *sdev)
339 {
340 	struct acp_dev_data *adata = sdev->pdata->hw_pdata;
341 	const struct sof_amd_acp_desc *desc = get_chip_info(sdev->pdata);
342 
343 	snd_sof_dsp_update_bits(sdev, ACP_DSP_BAR, desc->dsp_intr_base + DSP_SW_INTR_CNTL_OFFSET,
344 				ACP_DSP_INTR_EN_MASK, ACP_DSP_INTR_EN_MASK);
345 	init_dma_descriptor(adata);
346 
347 	return 0;
348 }
349 
350 static irqreturn_t acp_irq_thread(int irq, void *context)
351 {
352 	struct snd_sof_dev *sdev = context;
353 	const struct sof_amd_acp_desc *desc = get_chip_info(sdev->pdata);
354 	unsigned int count = ACP_HW_SEM_RETRY_COUNT;
355 
356 	spin_lock_irq(&sdev->ipc_lock);
357 	/* Wait until acquired HW Semaphore lock or timeout */
358 	while (snd_sof_dsp_read(sdev, ACP_DSP_BAR, desc->hw_semaphore_offset) && --count)
359 		;
360 	spin_unlock_irq(&sdev->ipc_lock);
361 
362 	if (!count) {
363 		dev_err(sdev->dev, "%s: Failed to acquire HW lock\n", __func__);
364 		return IRQ_NONE;
365 	}
366 
367 	sof_ops(sdev)->irq_thread(irq, sdev);
368 	/* Unlock or Release HW Semaphore */
369 	snd_sof_dsp_write(sdev, ACP_DSP_BAR, desc->hw_semaphore_offset, 0x0);
370 
371 	return IRQ_HANDLED;
372 };
373 
374 static irqreturn_t acp_irq_handler(int irq, void *dev_id)
375 {
376 	struct amd_sdw_manager *amd_manager;
377 	struct snd_sof_dev *sdev = dev_id;
378 	const struct sof_amd_acp_desc *desc = get_chip_info(sdev->pdata);
379 	struct acp_dev_data *adata = sdev->pdata->hw_pdata;
380 	unsigned int base = desc->dsp_intr_base;
381 	unsigned int val;
382 	int irq_flag = 0;
383 
384 	val = snd_sof_dsp_read(sdev, ACP_DSP_BAR, base + DSP_SW_INTR_STAT_OFFSET);
385 	if (val & ACP_DSP_TO_HOST_IRQ) {
386 		snd_sof_dsp_write(sdev, ACP_DSP_BAR, base + DSP_SW_INTR_STAT_OFFSET,
387 				  ACP_DSP_TO_HOST_IRQ);
388 		return IRQ_WAKE_THREAD;
389 	}
390 
391 	val = snd_sof_dsp_read(sdev, ACP_DSP_BAR, desc->ext_intr_stat);
392 	if (val & ACP_SDW0_IRQ_MASK) {
393 		amd_manager = dev_get_drvdata(&adata->sdw->pdev[0]->dev);
394 		snd_sof_dsp_write(sdev, ACP_DSP_BAR, desc->ext_intr_stat, ACP_SDW0_IRQ_MASK);
395 		if (amd_manager)
396 			schedule_work(&amd_manager->amd_sdw_irq_thread);
397 		irq_flag = 1;
398 	}
399 
400 	if (val & ACP_ERROR_IRQ_MASK) {
401 		snd_sof_dsp_write(sdev, ACP_DSP_BAR, desc->ext_intr_stat, ACP_ERROR_IRQ_MASK);
402 		snd_sof_dsp_write(sdev, ACP_DSP_BAR, base + ACP_SW0_I2S_ERROR_REASON, 0);
403 		snd_sof_dsp_write(sdev, ACP_DSP_BAR, base + ACP_SW1_I2S_ERROR_REASON, 0);
404 		snd_sof_dsp_write(sdev, ACP_DSP_BAR, base + ACP_ERROR_STATUS, 0);
405 		irq_flag = 1;
406 	}
407 
408 	if (desc->ext_intr_stat1) {
409 		val = snd_sof_dsp_read(sdev, ACP_DSP_BAR, desc->ext_intr_stat1);
410 		if (val & ACP_SDW1_IRQ_MASK) {
411 			amd_manager = dev_get_drvdata(&adata->sdw->pdev[1]->dev);
412 			snd_sof_dsp_write(sdev, ACP_DSP_BAR, desc->ext_intr_stat1,
413 					  ACP_SDW1_IRQ_MASK);
414 			if (amd_manager)
415 				schedule_work(&amd_manager->amd_sdw_irq_thread);
416 			irq_flag = 1;
417 		}
418 	}
419 	if (irq_flag)
420 		return IRQ_HANDLED;
421 	else
422 		return IRQ_NONE;
423 }
424 
425 static int acp_power_on(struct snd_sof_dev *sdev)
426 {
427 	const struct sof_amd_acp_desc *desc = get_chip_info(sdev->pdata);
428 	unsigned int base = desc->pgfsm_base;
429 	unsigned int val;
430 	int ret;
431 
432 	val = snd_sof_dsp_read(sdev, ACP_DSP_BAR, base + PGFSM_STATUS_OFFSET);
433 
434 	if (val == ACP_POWERED_ON)
435 		return 0;
436 
437 	if (val & ACP_PGFSM_STATUS_MASK)
438 		snd_sof_dsp_write(sdev, ACP_DSP_BAR, base + PGFSM_CONTROL_OFFSET,
439 				  ACP_PGFSM_CNTL_POWER_ON_MASK);
440 
441 	ret = snd_sof_dsp_read_poll_timeout(sdev, ACP_DSP_BAR, base + PGFSM_STATUS_OFFSET, val,
442 					    !val, ACP_REG_POLL_INTERVAL, ACP_REG_POLL_TIMEOUT_US);
443 	if (ret < 0)
444 		dev_err(sdev->dev, "timeout in ACP_PGFSM_STATUS read\n");
445 
446 	return ret;
447 }
448 
449 static int acp_reset(struct snd_sof_dev *sdev)
450 {
451 	const struct sof_amd_acp_desc *desc = get_chip_info(sdev->pdata);
452 	unsigned int val;
453 	int ret;
454 
455 	snd_sof_dsp_write(sdev, ACP_DSP_BAR, ACP_SOFT_RESET, ACP_ASSERT_RESET);
456 
457 	ret = snd_sof_dsp_read_poll_timeout(sdev, ACP_DSP_BAR, ACP_SOFT_RESET, val,
458 					    val & ACP_SOFT_RESET_DONE_MASK,
459 					    ACP_REG_POLL_INTERVAL, ACP_REG_POLL_TIMEOUT_US);
460 	if (ret < 0) {
461 		dev_err(sdev->dev, "timeout asserting reset\n");
462 		return ret;
463 	}
464 
465 	snd_sof_dsp_write(sdev, ACP_DSP_BAR, ACP_SOFT_RESET, ACP_RELEASE_RESET);
466 
467 	ret = snd_sof_dsp_read_poll_timeout(sdev, ACP_DSP_BAR, ACP_SOFT_RESET, val, !val,
468 					    ACP_REG_POLL_INTERVAL, ACP_REG_POLL_TIMEOUT_US);
469 	if (ret < 0)
470 		dev_err(sdev->dev, "timeout in releasing reset\n");
471 
472 	if (desc->acp_clkmux_sel)
473 		snd_sof_dsp_write(sdev, ACP_DSP_BAR, desc->acp_clkmux_sel, ACP_CLOCK_ACLK);
474 
475 	if (desc->ext_intr_enb)
476 		snd_sof_dsp_write(sdev, ACP_DSP_BAR, desc->ext_intr_enb, 0x01);
477 
478 	if (desc->ext_intr_cntl)
479 		snd_sof_dsp_write(sdev, ACP_DSP_BAR, desc->ext_intr_cntl, ACP_ERROR_IRQ_MASK);
480 	return ret;
481 }
482 
483 static int acp_dsp_reset(struct snd_sof_dev *sdev)
484 {
485 	unsigned int val;
486 	int ret;
487 
488 	snd_sof_dsp_write(sdev, ACP_DSP_BAR, ACP_SOFT_RESET, ACP_DSP_ASSERT_RESET);
489 
490 	ret = snd_sof_dsp_read_poll_timeout(sdev, ACP_DSP_BAR, ACP_SOFT_RESET, val,
491 					    val & ACP_DSP_SOFT_RESET_DONE_MASK,
492 					    ACP_REG_POLL_INTERVAL, ACP_REG_POLL_TIMEOUT_US);
493 	if (ret < 0) {
494 		dev_err(sdev->dev, "timeout asserting reset\n");
495 		return ret;
496 	}
497 
498 	snd_sof_dsp_write(sdev, ACP_DSP_BAR, ACP_SOFT_RESET, ACP_DSP_RELEASE_RESET);
499 
500 	ret = snd_sof_dsp_read_poll_timeout(sdev, ACP_DSP_BAR, ACP_SOFT_RESET, val, !val,
501 					    ACP_REG_POLL_INTERVAL, ACP_REG_POLL_TIMEOUT_US);
502 	if (ret < 0)
503 		dev_err(sdev->dev, "timeout in releasing reset\n");
504 
505 	return ret;
506 }
507 
508 static int acp_init(struct snd_sof_dev *sdev)
509 {
510 	int ret;
511 
512 	/* power on */
513 	ret = acp_power_on(sdev);
514 	if (ret) {
515 		dev_err(sdev->dev, "ACP power on failed\n");
516 		return ret;
517 	}
518 
519 	snd_sof_dsp_write(sdev, ACP_DSP_BAR, ACP_CONTROL, 0x01);
520 	/* Reset */
521 	return acp_reset(sdev);
522 }
523 
524 static bool check_acp_sdw_enable_status(struct snd_sof_dev *sdev)
525 {
526 	struct acp_dev_data *acp_data;
527 	u32 sdw0_en, sdw1_en;
528 
529 	acp_data = sdev->pdata->hw_pdata;
530 	if (!acp_data->sdw)
531 		return false;
532 
533 	sdw0_en = snd_sof_dsp_read(sdev, ACP_DSP_BAR, ACP_SW0_EN);
534 	sdw1_en = snd_sof_dsp_read(sdev, ACP_DSP_BAR, ACP_SW1_EN);
535 	acp_data->sdw_en_stat = sdw0_en || sdw1_en;
536 	return acp_data->sdw_en_stat;
537 }
538 
539 int amd_sof_acp_suspend(struct snd_sof_dev *sdev, u32 target_state)
540 {
541 	int ret;
542 
543 	/* When acp_reset() function is invoked, it will apply ACP SOFT reset and
544 	 * DSP reset. ACP Soft reset sequence will cause all ACP IP registers will
545 	 * be reset to default values which will break the ClockStop Mode functionality.
546 	 * Add a condition check to apply DSP reset when SoundWire ClockStop mode
547 	 * is selected. For the rest of the scenarios, apply acp reset sequence.
548 	 */
549 	if (check_acp_sdw_enable_status(sdev))
550 		return acp_dsp_reset(sdev);
551 
552 	ret = acp_reset(sdev);
553 	if (ret) {
554 		dev_err(sdev->dev, "ACP Reset failed\n");
555 		return ret;
556 	}
557 
558 	snd_sof_dsp_write(sdev, ACP_DSP_BAR, ACP_CONTROL, 0x00);
559 
560 	return 0;
561 }
562 EXPORT_SYMBOL_NS(amd_sof_acp_suspend, SND_SOC_SOF_AMD_COMMON);
563 
564 int amd_sof_acp_resume(struct snd_sof_dev *sdev)
565 {
566 	int ret;
567 	struct acp_dev_data *acp_data;
568 
569 	acp_data = sdev->pdata->hw_pdata;
570 	if (!acp_data->sdw_en_stat) {
571 		ret = acp_init(sdev);
572 		if (ret) {
573 			dev_err(sdev->dev, "ACP Init failed\n");
574 			return ret;
575 		}
576 		return acp_memory_init(sdev);
577 	} else {
578 		return acp_dsp_reset(sdev);
579 	}
580 }
581 EXPORT_SYMBOL_NS(amd_sof_acp_resume, SND_SOC_SOF_AMD_COMMON);
582 
583 #if IS_ENABLED(CONFIG_SND_SOC_SOF_AMD_SOUNDWIRE)
584 static int acp_sof_scan_sdw_devices(struct snd_sof_dev *sdev, u64 addr)
585 {
586 	struct acpi_device *sdw_dev;
587 	struct acp_dev_data *acp_data;
588 	const struct sof_amd_acp_desc *desc = get_chip_info(sdev->pdata);
589 
590 	if (!addr)
591 		return -ENODEV;
592 
593 	acp_data = sdev->pdata->hw_pdata;
594 	sdw_dev = acpi_find_child_device(ACPI_COMPANION(sdev->dev), addr, 0);
595 	if (!sdw_dev)
596 		return -ENODEV;
597 
598 	acp_data->info.handle = sdw_dev->handle;
599 	acp_data->info.count = desc->sdw_max_link_count;
600 
601 	return amd_sdw_scan_controller(&acp_data->info);
602 }
603 
604 static int amd_sof_sdw_probe(struct snd_sof_dev *sdev)
605 {
606 	struct acp_dev_data *acp_data;
607 	struct sdw_amd_res sdw_res;
608 	int ret;
609 
610 	acp_data = sdev->pdata->hw_pdata;
611 
612 	memset(&sdw_res, 0, sizeof(sdw_res));
613 	sdw_res.addr = acp_data->addr;
614 	sdw_res.reg_range = acp_data->reg_range;
615 	sdw_res.handle = acp_data->info.handle;
616 	sdw_res.parent = sdev->dev;
617 	sdw_res.dev = sdev->dev;
618 	sdw_res.acp_lock = &acp_data->acp_lock;
619 	sdw_res.count = acp_data->info.count;
620 	sdw_res.link_mask = acp_data->info.link_mask;
621 	sdw_res.mmio_base = sdev->bar[ACP_DSP_BAR];
622 
623 	ret = sdw_amd_probe(&sdw_res, &acp_data->sdw);
624 	if (ret)
625 		dev_err(sdev->dev, "SoundWire probe failed\n");
626 	return ret;
627 }
628 
629 static int amd_sof_sdw_exit(struct snd_sof_dev *sdev)
630 {
631 	struct acp_dev_data *acp_data;
632 
633 	acp_data = sdev->pdata->hw_pdata;
634 	if (acp_data->sdw)
635 		sdw_amd_exit(acp_data->sdw);
636 	acp_data->sdw = NULL;
637 
638 	return 0;
639 }
640 
641 #else
642 static int acp_sof_scan_sdw_devices(struct snd_sof_dev *sdev, u64 addr)
643 {
644 	return 0;
645 }
646 
647 static int amd_sof_sdw_probe(struct snd_sof_dev *sdev)
648 {
649 	return 0;
650 }
651 
652 static int amd_sof_sdw_exit(struct snd_sof_dev *sdev)
653 {
654 	return 0;
655 }
656 #endif
657 
658 int amd_sof_acp_probe(struct snd_sof_dev *sdev)
659 {
660 	struct pci_dev *pci = to_pci_dev(sdev->dev);
661 	struct acp_dev_data *adata;
662 	const struct sof_amd_acp_desc *chip;
663 	const struct dmi_system_id *dmi_id;
664 	unsigned int addr;
665 	int ret;
666 
667 	chip = get_chip_info(sdev->pdata);
668 	if (!chip) {
669 		dev_err(sdev->dev, "no such device supported, chip id:%x\n", pci->device);
670 		return -EIO;
671 	}
672 	adata = devm_kzalloc(sdev->dev, sizeof(struct acp_dev_data),
673 			     GFP_KERNEL);
674 	if (!adata)
675 		return -ENOMEM;
676 
677 	adata->dev = sdev;
678 	adata->dmic_dev = platform_device_register_data(sdev->dev, "dmic-codec",
679 							PLATFORM_DEVID_NONE, NULL, 0);
680 	if (IS_ERR(adata->dmic_dev)) {
681 		dev_err(sdev->dev, "failed to register platform for dmic codec\n");
682 		return PTR_ERR(adata->dmic_dev);
683 	}
684 	addr = pci_resource_start(pci, ACP_DSP_BAR);
685 	sdev->bar[ACP_DSP_BAR] = devm_ioremap(sdev->dev, addr, pci_resource_len(pci, ACP_DSP_BAR));
686 	if (!sdev->bar[ACP_DSP_BAR]) {
687 		dev_err(sdev->dev, "ioremap error\n");
688 		ret = -ENXIO;
689 		goto unregister_dev;
690 	}
691 
692 	pci_set_master(pci);
693 	adata->addr = addr;
694 	adata->reg_range = chip->reg_end_addr - chip->reg_start_addr;
695 	mutex_init(&adata->acp_lock);
696 	sdev->pdata->hw_pdata = adata;
697 	adata->smn_dev = pci_get_device(PCI_VENDOR_ID_AMD, chip->host_bridge_id, NULL);
698 	if (!adata->smn_dev) {
699 		dev_err(sdev->dev, "Failed to get host bridge device\n");
700 		ret = -ENODEV;
701 		goto unregister_dev;
702 	}
703 
704 	sdev->ipc_irq = pci->irq;
705 	ret = request_threaded_irq(sdev->ipc_irq, acp_irq_handler, acp_irq_thread,
706 				   IRQF_SHARED, "AudioDSP", sdev);
707 	if (ret < 0) {
708 		dev_err(sdev->dev, "failed to register IRQ %d\n",
709 			sdev->ipc_irq);
710 		goto free_smn_dev;
711 	}
712 
713 	ret = acp_init(sdev);
714 	if (ret < 0)
715 		goto free_ipc_irq;
716 
717 	/* scan SoundWire capabilities exposed by DSDT */
718 	ret = acp_sof_scan_sdw_devices(sdev, chip->sdw_acpi_dev_addr);
719 	if (ret < 0) {
720 		dev_dbg(sdev->dev, "skipping SoundWire, not detected with ACPI scan\n");
721 		goto skip_soundwire;
722 	}
723 	ret = amd_sof_sdw_probe(sdev);
724 	if (ret < 0) {
725 		dev_err(sdev->dev, "error: SoundWire probe error\n");
726 		free_irq(sdev->ipc_irq, sdev);
727 		pci_dev_put(adata->smn_dev);
728 		return ret;
729 	}
730 
731 skip_soundwire:
732 	sdev->dsp_box.offset = 0;
733 	sdev->dsp_box.size = BOX_SIZE_512;
734 
735 	sdev->host_box.offset = sdev->dsp_box.offset + sdev->dsp_box.size;
736 	sdev->host_box.size = BOX_SIZE_512;
737 
738 	sdev->debug_box.offset = sdev->host_box.offset + sdev->host_box.size;
739 	sdev->debug_box.size = BOX_SIZE_1024;
740 
741 	adata->signed_fw_image = false;
742 	dmi_id = dmi_first_match(acp_sof_quirk_table);
743 	if (dmi_id && dmi_id->driver_data) {
744 		adata->fw_code_bin = devm_kasprintf(sdev->dev, GFP_KERNEL,
745 						    "sof-%s-code.bin",
746 						    chip->name);
747 		if (!adata->fw_code_bin) {
748 			ret = -ENOMEM;
749 			goto free_ipc_irq;
750 		}
751 
752 		adata->fw_data_bin = devm_kasprintf(sdev->dev, GFP_KERNEL,
753 						    "sof-%s-data.bin",
754 						    chip->name);
755 		if (!adata->fw_data_bin) {
756 			ret = -ENOMEM;
757 			goto free_ipc_irq;
758 		}
759 
760 		adata->signed_fw_image = dmi_id->driver_data;
761 	}
762 
763 	adata->enable_fw_debug = enable_fw_debug;
764 	acp_memory_init(sdev);
765 
766 	acp_dsp_stream_init(sdev);
767 
768 	return 0;
769 
770 free_ipc_irq:
771 	free_irq(sdev->ipc_irq, sdev);
772 free_smn_dev:
773 	pci_dev_put(adata->smn_dev);
774 unregister_dev:
775 	platform_device_unregister(adata->dmic_dev);
776 	return ret;
777 }
778 EXPORT_SYMBOL_NS(amd_sof_acp_probe, SND_SOC_SOF_AMD_COMMON);
779 
780 void amd_sof_acp_remove(struct snd_sof_dev *sdev)
781 {
782 	struct acp_dev_data *adata = sdev->pdata->hw_pdata;
783 
784 	if (adata->smn_dev)
785 		pci_dev_put(adata->smn_dev);
786 
787 	if (adata->sdw)
788 		amd_sof_sdw_exit(sdev);
789 
790 	if (sdev->ipc_irq)
791 		free_irq(sdev->ipc_irq, sdev);
792 
793 	if (adata->dmic_dev)
794 		platform_device_unregister(adata->dmic_dev);
795 
796 	acp_reset(sdev);
797 }
798 EXPORT_SYMBOL_NS(amd_sof_acp_remove, SND_SOC_SOF_AMD_COMMON);
799 
800 MODULE_DESCRIPTION("AMD ACP sof driver");
801 MODULE_IMPORT_NS(SOUNDWIRE_AMD_INIT);
802 MODULE_IMPORT_NS(SND_AMD_SOUNDWIRE_ACPI);
803 MODULE_LICENSE("Dual BSD/GPL");
804