1*4882a593Smuzhiyun // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2*4882a593Smuzhiyun //
3*4882a593Smuzhiyun // This file is provided under a dual BSD/GPLv2 license. When using or
4*4882a593Smuzhiyun // redistributing this file, you may do so under either license.
5*4882a593Smuzhiyun //
6*4882a593Smuzhiyun // Copyright(c) 2018 Intel Corporation. All rights reserved.
7*4882a593Smuzhiyun //
8*4882a593Smuzhiyun // Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9*4882a593Smuzhiyun // Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
10*4882a593Smuzhiyun // Rander Wang <rander.wang@intel.com>
11*4882a593Smuzhiyun // Keyon Jie <yang.jie@linux.intel.com>
12*4882a593Smuzhiyun //
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun /*
15*4882a593Smuzhiyun * Hardware interface for generic Intel audio DSP HDA IP
16*4882a593Smuzhiyun */
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #include "../ops.h"
19*4882a593Smuzhiyun #include "hda.h"
20*4882a593Smuzhiyun
hda_dsp_ipc_host_done(struct snd_sof_dev * sdev)21*4882a593Smuzhiyun static void hda_dsp_ipc_host_done(struct snd_sof_dev *sdev)
22*4882a593Smuzhiyun {
23*4882a593Smuzhiyun /*
24*4882a593Smuzhiyun * tell DSP cmd is done - clear busy
25*4882a593Smuzhiyun * interrupt and send reply msg to dsp
26*4882a593Smuzhiyun */
27*4882a593Smuzhiyun snd_sof_dsp_update_bits_forced(sdev, HDA_DSP_BAR,
28*4882a593Smuzhiyun HDA_DSP_REG_HIPCT,
29*4882a593Smuzhiyun HDA_DSP_REG_HIPCT_BUSY,
30*4882a593Smuzhiyun HDA_DSP_REG_HIPCT_BUSY);
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun /* unmask BUSY interrupt */
33*4882a593Smuzhiyun snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR,
34*4882a593Smuzhiyun HDA_DSP_REG_HIPCCTL,
35*4882a593Smuzhiyun HDA_DSP_REG_HIPCCTL_BUSY,
36*4882a593Smuzhiyun HDA_DSP_REG_HIPCCTL_BUSY);
37*4882a593Smuzhiyun }
38*4882a593Smuzhiyun
hda_dsp_ipc_dsp_done(struct snd_sof_dev * sdev)39*4882a593Smuzhiyun static void hda_dsp_ipc_dsp_done(struct snd_sof_dev *sdev)
40*4882a593Smuzhiyun {
41*4882a593Smuzhiyun /*
42*4882a593Smuzhiyun * set DONE bit - tell DSP we have received the reply msg
43*4882a593Smuzhiyun * from DSP, and processed it, don't send more reply to host
44*4882a593Smuzhiyun */
45*4882a593Smuzhiyun snd_sof_dsp_update_bits_forced(sdev, HDA_DSP_BAR,
46*4882a593Smuzhiyun HDA_DSP_REG_HIPCIE,
47*4882a593Smuzhiyun HDA_DSP_REG_HIPCIE_DONE,
48*4882a593Smuzhiyun HDA_DSP_REG_HIPCIE_DONE);
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun /* unmask Done interrupt */
51*4882a593Smuzhiyun snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR,
52*4882a593Smuzhiyun HDA_DSP_REG_HIPCCTL,
53*4882a593Smuzhiyun HDA_DSP_REG_HIPCCTL_DONE,
54*4882a593Smuzhiyun HDA_DSP_REG_HIPCCTL_DONE);
55*4882a593Smuzhiyun }
56*4882a593Smuzhiyun
hda_dsp_ipc_send_msg(struct snd_sof_dev * sdev,struct snd_sof_ipc_msg * msg)57*4882a593Smuzhiyun int hda_dsp_ipc_send_msg(struct snd_sof_dev *sdev, struct snd_sof_ipc_msg *msg)
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun /* send IPC message to DSP */
60*4882a593Smuzhiyun sof_mailbox_write(sdev, sdev->host_box.offset, msg->msg_data,
61*4882a593Smuzhiyun msg->msg_size);
62*4882a593Smuzhiyun snd_sof_dsp_write(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCI,
63*4882a593Smuzhiyun HDA_DSP_REG_HIPCI_BUSY);
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun return 0;
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun
hda_dsp_ipc_get_reply(struct snd_sof_dev * sdev)68*4882a593Smuzhiyun void hda_dsp_ipc_get_reply(struct snd_sof_dev *sdev)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun struct snd_sof_ipc_msg *msg = sdev->msg;
71*4882a593Smuzhiyun struct sof_ipc_reply reply;
72*4882a593Smuzhiyun struct sof_ipc_cmd_hdr *hdr;
73*4882a593Smuzhiyun int ret = 0;
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun /*
76*4882a593Smuzhiyun * Sometimes, there is unexpected reply ipc arriving. The reply
77*4882a593Smuzhiyun * ipc belongs to none of the ipcs sent from driver.
78*4882a593Smuzhiyun * In this case, the driver must ignore the ipc.
79*4882a593Smuzhiyun */
80*4882a593Smuzhiyun if (!msg) {
81*4882a593Smuzhiyun dev_warn(sdev->dev, "unexpected ipc interrupt raised!\n");
82*4882a593Smuzhiyun return;
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun hdr = msg->msg_data;
86*4882a593Smuzhiyun if (hdr->cmd == (SOF_IPC_GLB_PM_MSG | SOF_IPC_PM_CTX_SAVE) ||
87*4882a593Smuzhiyun hdr->cmd == (SOF_IPC_GLB_PM_MSG | SOF_IPC_PM_GATE)) {
88*4882a593Smuzhiyun /*
89*4882a593Smuzhiyun * memory windows are powered off before sending IPC reply,
90*4882a593Smuzhiyun * so we can't read the mailbox for CTX_SAVE and PM_GATE
91*4882a593Smuzhiyun * replies.
92*4882a593Smuzhiyun */
93*4882a593Smuzhiyun reply.error = 0;
94*4882a593Smuzhiyun reply.hdr.cmd = SOF_IPC_GLB_REPLY;
95*4882a593Smuzhiyun reply.hdr.size = sizeof(reply);
96*4882a593Smuzhiyun memcpy(msg->reply_data, &reply, sizeof(reply));
97*4882a593Smuzhiyun goto out;
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun /* get IPC reply from DSP in the mailbox */
101*4882a593Smuzhiyun sof_mailbox_read(sdev, sdev->host_box.offset, &reply,
102*4882a593Smuzhiyun sizeof(reply));
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun if (reply.error < 0) {
105*4882a593Smuzhiyun memcpy(msg->reply_data, &reply, sizeof(reply));
106*4882a593Smuzhiyun ret = reply.error;
107*4882a593Smuzhiyun } else {
108*4882a593Smuzhiyun /* reply correct size ? */
109*4882a593Smuzhiyun if (reply.hdr.size != msg->reply_size &&
110*4882a593Smuzhiyun /* getter payload is never known upfront */
111*4882a593Smuzhiyun ((reply.hdr.cmd & SOF_GLB_TYPE_MASK) != SOF_IPC_GLB_PROBE)) {
112*4882a593Smuzhiyun dev_err(sdev->dev, "error: reply expected %zu got %u bytes\n",
113*4882a593Smuzhiyun msg->reply_size, reply.hdr.size);
114*4882a593Smuzhiyun ret = -EINVAL;
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun /* read the message */
118*4882a593Smuzhiyun if (msg->reply_size > 0)
119*4882a593Smuzhiyun sof_mailbox_read(sdev, sdev->host_box.offset,
120*4882a593Smuzhiyun msg->reply_data, msg->reply_size);
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun out:
124*4882a593Smuzhiyun msg->reply_error = ret;
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun /* IPC handler thread */
hda_dsp_ipc_irq_thread(int irq,void * context)129*4882a593Smuzhiyun irqreturn_t hda_dsp_ipc_irq_thread(int irq, void *context)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun struct snd_sof_dev *sdev = context;
132*4882a593Smuzhiyun u32 hipci;
133*4882a593Smuzhiyun u32 hipcie;
134*4882a593Smuzhiyun u32 hipct;
135*4882a593Smuzhiyun u32 hipcte;
136*4882a593Smuzhiyun u32 msg;
137*4882a593Smuzhiyun u32 msg_ext;
138*4882a593Smuzhiyun bool ipc_irq = false;
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun /* read IPC status */
141*4882a593Smuzhiyun hipcie = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
142*4882a593Smuzhiyun HDA_DSP_REG_HIPCIE);
143*4882a593Smuzhiyun hipct = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCT);
144*4882a593Smuzhiyun hipci = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCI);
145*4882a593Smuzhiyun hipcte = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_HIPCTE);
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun /* is this a reply message from the DSP */
148*4882a593Smuzhiyun if (hipcie & HDA_DSP_REG_HIPCIE_DONE) {
149*4882a593Smuzhiyun msg = hipci & HDA_DSP_REG_HIPCI_MSG_MASK;
150*4882a593Smuzhiyun msg_ext = hipcie & HDA_DSP_REG_HIPCIE_MSG_MASK;
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun dev_vdbg(sdev->dev,
153*4882a593Smuzhiyun "ipc: firmware response, msg:0x%x, msg_ext:0x%x\n",
154*4882a593Smuzhiyun msg, msg_ext);
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun /* mask Done interrupt */
157*4882a593Smuzhiyun snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR,
158*4882a593Smuzhiyun HDA_DSP_REG_HIPCCTL,
159*4882a593Smuzhiyun HDA_DSP_REG_HIPCCTL_DONE, 0);
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun /*
162*4882a593Smuzhiyun * Make sure the interrupt thread cannot be preempted between
163*4882a593Smuzhiyun * waking up the sender and re-enabling the interrupt. Also
164*4882a593Smuzhiyun * protect against a theoretical race with sof_ipc_tx_message():
165*4882a593Smuzhiyun * if the DSP is fast enough to receive an IPC message, reply to
166*4882a593Smuzhiyun * it, and the host interrupt processing calls this function on
167*4882a593Smuzhiyun * a different core from the one, where the sending is taking
168*4882a593Smuzhiyun * place, the message might not yet be marked as expecting a
169*4882a593Smuzhiyun * reply.
170*4882a593Smuzhiyun */
171*4882a593Smuzhiyun spin_lock_irq(&sdev->ipc_lock);
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun /* handle immediate reply from DSP core */
174*4882a593Smuzhiyun hda_dsp_ipc_get_reply(sdev);
175*4882a593Smuzhiyun snd_sof_ipc_reply(sdev, msg);
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun /* set the done bit */
178*4882a593Smuzhiyun hda_dsp_ipc_dsp_done(sdev);
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun spin_unlock_irq(&sdev->ipc_lock);
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun ipc_irq = true;
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun /* is this a new message from DSP */
186*4882a593Smuzhiyun if (hipct & HDA_DSP_REG_HIPCT_BUSY) {
187*4882a593Smuzhiyun msg = hipct & HDA_DSP_REG_HIPCT_MSG_MASK;
188*4882a593Smuzhiyun msg_ext = hipcte & HDA_DSP_REG_HIPCTE_MSG_MASK;
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun dev_vdbg(sdev->dev,
191*4882a593Smuzhiyun "ipc: firmware initiated, msg:0x%x, msg_ext:0x%x\n",
192*4882a593Smuzhiyun msg, msg_ext);
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun /* mask BUSY interrupt */
195*4882a593Smuzhiyun snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR,
196*4882a593Smuzhiyun HDA_DSP_REG_HIPCCTL,
197*4882a593Smuzhiyun HDA_DSP_REG_HIPCCTL_BUSY, 0);
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun /* handle messages from DSP */
200*4882a593Smuzhiyun if ((hipct & SOF_IPC_PANIC_MAGIC_MASK) == SOF_IPC_PANIC_MAGIC) {
201*4882a593Smuzhiyun /* this is a PANIC message !! */
202*4882a593Smuzhiyun snd_sof_dsp_panic(sdev, HDA_DSP_PANIC_OFFSET(msg_ext));
203*4882a593Smuzhiyun } else {
204*4882a593Smuzhiyun /* normal message - process normally */
205*4882a593Smuzhiyun snd_sof_ipc_msgs_rx(sdev);
206*4882a593Smuzhiyun }
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun hda_dsp_ipc_host_done(sdev);
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun ipc_irq = true;
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun if (!ipc_irq) {
214*4882a593Smuzhiyun /*
215*4882a593Smuzhiyun * This interrupt is not shared so no need to return IRQ_NONE.
216*4882a593Smuzhiyun */
217*4882a593Smuzhiyun dev_dbg_ratelimited(sdev->dev,
218*4882a593Smuzhiyun "nothing to do in IPC IRQ thread\n");
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun return IRQ_HANDLED;
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun /* Check if an IPC IRQ occurred */
hda_dsp_check_ipc_irq(struct snd_sof_dev * sdev)225*4882a593Smuzhiyun bool hda_dsp_check_ipc_irq(struct snd_sof_dev *sdev)
226*4882a593Smuzhiyun {
227*4882a593Smuzhiyun bool ret = false;
228*4882a593Smuzhiyun u32 irq_status;
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun /* store status */
231*4882a593Smuzhiyun irq_status = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIS);
232*4882a593Smuzhiyun dev_vdbg(sdev->dev, "irq handler: irq_status:0x%x\n", irq_status);
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun /* invalid message ? */
235*4882a593Smuzhiyun if (irq_status == 0xffffffff)
236*4882a593Smuzhiyun goto out;
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun /* IPC message ? */
239*4882a593Smuzhiyun if (irq_status & HDA_DSP_ADSPIS_IPC)
240*4882a593Smuzhiyun ret = true;
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun out:
243*4882a593Smuzhiyun return ret;
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun
hda_dsp_ipc_get_mailbox_offset(struct snd_sof_dev * sdev)246*4882a593Smuzhiyun int hda_dsp_ipc_get_mailbox_offset(struct snd_sof_dev *sdev)
247*4882a593Smuzhiyun {
248*4882a593Smuzhiyun return HDA_DSP_MBOX_UPLINK_OFFSET;
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun
hda_dsp_ipc_get_window_offset(struct snd_sof_dev * sdev,u32 id)251*4882a593Smuzhiyun int hda_dsp_ipc_get_window_offset(struct snd_sof_dev *sdev, u32 id)
252*4882a593Smuzhiyun {
253*4882a593Smuzhiyun return SRAM_WINDOW_OFFSET(id);
254*4882a593Smuzhiyun }
255*4882a593Smuzhiyun
hda_ipc_msg_data(struct snd_sof_dev * sdev,struct snd_pcm_substream * substream,void * p,size_t sz)256*4882a593Smuzhiyun void hda_ipc_msg_data(struct snd_sof_dev *sdev,
257*4882a593Smuzhiyun struct snd_pcm_substream *substream,
258*4882a593Smuzhiyun void *p, size_t sz)
259*4882a593Smuzhiyun {
260*4882a593Smuzhiyun if (!substream || !sdev->stream_box.size) {
261*4882a593Smuzhiyun sof_mailbox_read(sdev, sdev->dsp_box.offset, p, sz);
262*4882a593Smuzhiyun } else {
263*4882a593Smuzhiyun struct hdac_stream *hstream = substream->runtime->private_data;
264*4882a593Smuzhiyun struct sof_intel_hda_stream *hda_stream;
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun hda_stream = container_of(hstream,
267*4882a593Smuzhiyun struct sof_intel_hda_stream,
268*4882a593Smuzhiyun hda_stream.hstream);
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun /* The stream might already be closed */
271*4882a593Smuzhiyun if (hstream)
272*4882a593Smuzhiyun sof_mailbox_read(sdev, hda_stream->stream.posn_offset,
273*4882a593Smuzhiyun p, sz);
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun }
276*4882a593Smuzhiyun
hda_ipc_pcm_params(struct snd_sof_dev * sdev,struct snd_pcm_substream * substream,const struct sof_ipc_pcm_params_reply * reply)277*4882a593Smuzhiyun int hda_ipc_pcm_params(struct snd_sof_dev *sdev,
278*4882a593Smuzhiyun struct snd_pcm_substream *substream,
279*4882a593Smuzhiyun const struct sof_ipc_pcm_params_reply *reply)
280*4882a593Smuzhiyun {
281*4882a593Smuzhiyun struct hdac_stream *hstream = substream->runtime->private_data;
282*4882a593Smuzhiyun struct sof_intel_hda_stream *hda_stream;
283*4882a593Smuzhiyun /* validate offset */
284*4882a593Smuzhiyun size_t posn_offset = reply->posn_offset;
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun hda_stream = container_of(hstream, struct sof_intel_hda_stream,
287*4882a593Smuzhiyun hda_stream.hstream);
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun /* check for unaligned offset or overflow */
290*4882a593Smuzhiyun if (posn_offset > sdev->stream_box.size ||
291*4882a593Smuzhiyun posn_offset % sizeof(struct sof_ipc_stream_posn) != 0)
292*4882a593Smuzhiyun return -EINVAL;
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun hda_stream->stream.posn_offset = sdev->stream_box.offset + posn_offset;
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun dev_dbg(sdev->dev, "pcm: stream dir %d, posn mailbox offset is %zu",
297*4882a593Smuzhiyun substream->stream, hda_stream->stream.posn_offset);
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun return 0;
300*4882a593Smuzhiyun }
301