1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
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.
6 // Copyright(c) 2018 Intel Corporation
8 // Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9 // Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
10 // Rander Wang <rander.wang@intel.com>
11 // Keyon Jie <yang.jie@linux.intel.com>
15 * Hardware interface for generic Intel audio DSP HDA IP
18 #include <sound/sof/ipc4/header.h>
19 #include <trace/events/sof_intel.h>
23 static void hda_dsp_ipc_host_done(struct snd_sof_dev *sdev)
26 * tell DSP cmd is done - clear busy
27 * interrupt and send reply msg to dsp
29 snd_sof_dsp_update_bits_forced(sdev, HDA_DSP_BAR,
31 HDA_DSP_REG_HIPCT_BUSY,
32 HDA_DSP_REG_HIPCT_BUSY);
34 /* unmask BUSY interrupt */
35 snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR,
37 HDA_DSP_REG_HIPCCTL_BUSY,
38 HDA_DSP_REG_HIPCCTL_BUSY);
41 static void hda_dsp_ipc_dsp_done(struct snd_sof_dev *sdev)
44 * set DONE bit - tell DSP we have received the reply msg
45 * from DSP, and processed it, don't send more reply to host
47 snd_sof_dsp_update_bits_forced(sdev, HDA_DSP_BAR,
49 HDA_DSP_REG_HIPCIE_DONE,
50 HDA_DSP_REG_HIPCIE_DONE);
52 /* unmask Done interrupt */
53 snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR,
55 HDA_DSP_REG_HIPCCTL_DONE,
56 HDA_DSP_REG_HIPCCTL_DONE);
59 int hda_dsp_ipc_send_msg(struct snd_sof_dev *sdev, struct snd_sof_ipc_msg *msg)
61 /* send IPC message to DSP */
62 sof_mailbox_write(sdev, sdev->host_box.offset, msg->msg_data,
64 snd_sof_dsp_write(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCI,
65 HDA_DSP_REG_HIPCI_BUSY);
70 static inline bool hda_dsp_ipc4_pm_msg(u32 primary)
72 /* pm setting is only supported by module msg */
73 if (SOF_IPC4_MSG_IS_MODULE_MSG(primary) != SOF_IPC4_MODULE_MSG)
76 if (SOF_IPC4_MSG_TYPE_GET(primary) == SOF_IPC4_MOD_SET_DX ||
77 SOF_IPC4_MSG_TYPE_GET(primary) == SOF_IPC4_MOD_SET_D0IX)
83 void hda_dsp_ipc4_schedule_d0i3_work(struct sof_intel_hda_dev *hdev,
84 struct snd_sof_ipc_msg *msg)
86 struct sof_ipc4_msg *msg_data = msg->msg_data;
88 /* Schedule a delayed work for d0i3 entry after sending non-pm ipc msg */
89 if (hda_dsp_ipc4_pm_msg(msg_data->primary))
92 mod_delayed_work(system_wq, &hdev->d0i3_work,
93 msecs_to_jiffies(SOF_HDA_D0I3_WORK_DELAY_MS));
96 int hda_dsp_ipc4_send_msg(struct snd_sof_dev *sdev, struct snd_sof_ipc_msg *msg)
98 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
99 struct sof_ipc4_msg *msg_data = msg->msg_data;
101 if (hda_ipc4_tx_is_busy(sdev)) {
102 hdev->delayed_ipc_tx_msg = msg;
106 hdev->delayed_ipc_tx_msg = NULL;
108 /* send the message via mailbox */
109 if (msg_data->data_size)
110 sof_mailbox_write(sdev, sdev->host_box.offset, msg_data->data_ptr,
111 msg_data->data_size);
113 snd_sof_dsp_write(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCIE, msg_data->extension);
114 snd_sof_dsp_write(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCI,
115 msg_data->primary | HDA_DSP_REG_HIPCI_BUSY);
117 hda_dsp_ipc4_schedule_d0i3_work(hdev, msg);
122 void hda_dsp_ipc_get_reply(struct snd_sof_dev *sdev)
124 struct snd_sof_ipc_msg *msg = sdev->msg;
125 struct sof_ipc_reply reply;
126 struct sof_ipc_cmd_hdr *hdr;
129 * Sometimes, there is unexpected reply ipc arriving. The reply
130 * ipc belongs to none of the ipcs sent from driver.
131 * In this case, the driver must ignore the ipc.
134 dev_warn(sdev->dev, "unexpected ipc interrupt raised!\n");
139 if (hdr->cmd == (SOF_IPC_GLB_PM_MSG | SOF_IPC_PM_CTX_SAVE) ||
140 hdr->cmd == (SOF_IPC_GLB_PM_MSG | SOF_IPC_PM_GATE)) {
142 * memory windows are powered off before sending IPC reply,
143 * so we can't read the mailbox for CTX_SAVE and PM_GATE
147 reply.hdr.cmd = SOF_IPC_GLB_REPLY;
148 reply.hdr.size = sizeof(reply);
149 memcpy(msg->reply_data, &reply, sizeof(reply));
151 msg->reply_error = 0;
153 snd_sof_ipc_get_reply(sdev);
157 irqreturn_t hda_dsp_ipc4_irq_thread(int irq, void *context)
159 struct sof_ipc4_msg notification_data = {{ 0 }};
160 struct snd_sof_dev *sdev = context;
161 bool ack_received = false;
162 bool ipc_irq = false;
165 hipcie = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCIE);
166 hipct = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCT);
168 if (hipcie & HDA_DSP_REG_HIPCIE_DONE) {
169 /* DSP received the message */
170 snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCCTL,
171 HDA_DSP_REG_HIPCCTL_DONE, 0);
172 hda_dsp_ipc_dsp_done(sdev);
178 if (hipct & HDA_DSP_REG_HIPCT_BUSY) {
179 /* Message from DSP (reply or notification) */
180 u32 hipcte = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
182 u32 primary = hipct & HDA_DSP_REG_HIPCT_MSG_MASK;
183 u32 extension = hipcte & HDA_DSP_REG_HIPCTE_MSG_MASK;
185 /* mask BUSY interrupt */
186 snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCCTL,
187 HDA_DSP_REG_HIPCCTL_BUSY, 0);
189 if (primary & SOF_IPC4_MSG_DIR_MASK) {
191 if (likely(sdev->fw_state == SOF_FW_BOOT_COMPLETE)) {
192 struct sof_ipc4_msg *data = sdev->ipc->msg.reply_data;
194 data->primary = primary;
195 data->extension = extension;
197 spin_lock_irq(&sdev->ipc_lock);
199 snd_sof_ipc_get_reply(sdev);
200 hda_dsp_ipc_host_done(sdev);
201 snd_sof_ipc_reply(sdev, data->primary);
203 spin_unlock_irq(&sdev->ipc_lock);
205 dev_dbg_ratelimited(sdev->dev,
206 "IPC reply before FW_READY: %#x|%#x\n",
210 /* Notification received */
212 notification_data.primary = primary;
213 notification_data.extension = extension;
214 sdev->ipc->msg.rx_data = ¬ification_data;
215 snd_sof_ipc_msgs_rx(sdev);
216 sdev->ipc->msg.rx_data = NULL;
218 /* Let DSP know that we have finished processing the message */
219 hda_dsp_ipc_host_done(sdev);
226 /* This interrupt is not shared so no need to return IRQ_NONE. */
227 dev_dbg_ratelimited(sdev->dev, "nothing to do in IPC IRQ thread\n");
230 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
232 if (hdev->delayed_ipc_tx_msg)
233 hda_dsp_ipc4_send_msg(sdev, hdev->delayed_ipc_tx_msg);
239 /* IPC handler thread */
240 irqreturn_t hda_dsp_ipc_irq_thread(int irq, void *context)
242 struct snd_sof_dev *sdev = context;
249 bool ipc_irq = false;
251 /* read IPC status */
252 hipcie = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
254 hipct = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCT);
255 hipci = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCI);
256 hipcte = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCTE);
258 /* is this a reply message from the DSP */
259 if (hipcie & HDA_DSP_REG_HIPCIE_DONE) {
260 msg = hipci & HDA_DSP_REG_HIPCI_MSG_MASK;
261 msg_ext = hipcie & HDA_DSP_REG_HIPCIE_MSG_MASK;
263 trace_sof_intel_ipc_firmware_response(sdev, msg, msg_ext);
265 /* mask Done interrupt */
266 snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR,
268 HDA_DSP_REG_HIPCCTL_DONE, 0);
271 * Make sure the interrupt thread cannot be preempted between
272 * waking up the sender and re-enabling the interrupt. Also
273 * protect against a theoretical race with sof_ipc_tx_message():
274 * if the DSP is fast enough to receive an IPC message, reply to
275 * it, and the host interrupt processing calls this function on
276 * a different core from the one, where the sending is taking
277 * place, the message might not yet be marked as expecting a
280 if (likely(sdev->fw_state == SOF_FW_BOOT_COMPLETE)) {
281 spin_lock_irq(&sdev->ipc_lock);
283 /* handle immediate reply from DSP core */
284 hda_dsp_ipc_get_reply(sdev);
285 snd_sof_ipc_reply(sdev, msg);
287 /* set the done bit */
288 hda_dsp_ipc_dsp_done(sdev);
290 spin_unlock_irq(&sdev->ipc_lock);
292 dev_dbg_ratelimited(sdev->dev, "IPC reply before FW_READY: %#x\n",
299 /* is this a new message from DSP */
300 if (hipct & HDA_DSP_REG_HIPCT_BUSY) {
301 msg = hipct & HDA_DSP_REG_HIPCT_MSG_MASK;
302 msg_ext = hipcte & HDA_DSP_REG_HIPCTE_MSG_MASK;
304 trace_sof_intel_ipc_firmware_initiated(sdev, msg, msg_ext);
306 /* mask BUSY interrupt */
307 snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR,
309 HDA_DSP_REG_HIPCCTL_BUSY, 0);
311 /* handle messages from DSP */
312 if ((hipct & SOF_IPC_PANIC_MAGIC_MASK) == SOF_IPC_PANIC_MAGIC) {
313 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
314 bool non_recoverable = true;
317 * This is a PANIC message!
319 * If it is arriving during firmware boot and it is not
320 * the last boot attempt then change the non_recoverable
321 * to false as the DSP might be able to boot in the next
324 if (sdev->fw_state == SOF_FW_BOOT_IN_PROGRESS &&
325 hda->boot_iteration < HDA_FW_BOOT_ATTEMPTS)
326 non_recoverable = false;
328 snd_sof_dsp_panic(sdev, HDA_DSP_PANIC_OFFSET(msg_ext),
331 /* normal message - process normally */
332 snd_sof_ipc_msgs_rx(sdev);
335 hda_dsp_ipc_host_done(sdev);
342 * This interrupt is not shared so no need to return IRQ_NONE.
344 dev_dbg_ratelimited(sdev->dev,
345 "nothing to do in IPC IRQ thread\n");
351 /* Check if an IPC IRQ occurred */
352 bool hda_dsp_check_ipc_irq(struct snd_sof_dev *sdev)
354 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
358 if (sdev->dspless_mode_selected)
362 irq_status = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIS);
363 trace_sof_intel_hda_irq_ipc_check(sdev, irq_status);
365 /* invalid message ? */
366 if (irq_status == 0xffffffff)
370 if (irq_status & HDA_DSP_ADSPIS_IPC)
373 /* CLDMA message ? */
374 if (irq_status & HDA_DSP_ADSPIS_CL_DMA) {
375 hda->code_loading = 0;
376 wake_up(&hda->waitq);
384 int hda_dsp_ipc_get_mailbox_offset(struct snd_sof_dev *sdev)
386 return HDA_DSP_MBOX_UPLINK_OFFSET;
389 int hda_dsp_ipc_get_window_offset(struct snd_sof_dev *sdev, u32 id)
391 return SRAM_WINDOW_OFFSET(id);
394 int hda_ipc_msg_data(struct snd_sof_dev *sdev,
395 struct snd_sof_pcm_stream *sps,
398 if (!sps || !sdev->stream_box.size) {
399 sof_mailbox_read(sdev, sdev->dsp_box.offset, p, sz);
401 struct snd_pcm_substream *substream = sps->substream;
402 struct hdac_stream *hstream = substream->runtime->private_data;
403 struct sof_intel_hda_stream *hda_stream;
405 hda_stream = container_of(hstream,
406 struct sof_intel_hda_stream,
407 hext_stream.hstream);
409 /* The stream might already be closed */
413 sof_mailbox_read(sdev, hda_stream->sof_intel_stream.posn_offset, p, sz);
419 int hda_set_stream_data_offset(struct snd_sof_dev *sdev,
420 struct snd_sof_pcm_stream *sps,
423 struct snd_pcm_substream *substream = sps->substream;
424 struct hdac_stream *hstream = substream->runtime->private_data;
425 struct sof_intel_hda_stream *hda_stream;
427 hda_stream = container_of(hstream, struct sof_intel_hda_stream,
428 hext_stream.hstream);
430 /* check for unaligned offset or overflow */
431 if (posn_offset > sdev->stream_box.size ||
432 posn_offset % sizeof(struct sof_ipc_stream_posn) != 0)
435 hda_stream->sof_intel_stream.posn_offset = sdev->stream_box.offset + posn_offset;
437 dev_dbg(sdev->dev, "pcm: stream dir %d, posn mailbox offset is %zu",
438 substream->stream, hda_stream->sof_intel_stream.posn_offset);