xref: /linux/sound/soc/intel/catpt/ipc.c (revision 0fc8f6200d2313278fbf4539bbab74677c685531)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2020 Intel Corporation
4 //
5 // Author: Cezary Rojewski <cezary.rojewski@intel.com>
6 //
7 
8 #include <linux/cleanup.h>
9 #include <linux/irqreturn.h>
10 #include "core.h"
11 #include "messages.h"
12 #include "registers.h"
13 #include "trace.h"
14 
15 #define CATPT_IPC_TIMEOUT_MS	300
16 
17 void catpt_ipc_init(struct catpt_ipc *ipc, struct device *dev)
18 {
19 	ipc->dev = dev;
20 	ipc->ready = false;
21 	ipc->default_timeout = CATPT_IPC_TIMEOUT_MS;
22 	init_completion(&ipc->done_completion);
23 	init_completion(&ipc->busy_completion);
24 	spin_lock_init(&ipc->lock);
25 	mutex_init(&ipc->mutex);
26 }
27 
28 static int catpt_ipc_arm(struct catpt_ipc *ipc, struct catpt_fw_ready *config)
29 {
30 	/*
31 	 * Both tx and rx are put into and received from outbox. Inbox is
32 	 * only used for notifications where payload size is known upfront,
33 	 * thus no separate buffer is allocated for it.
34 	 */
35 	ipc->rx.data = devm_kzalloc(ipc->dev, config->outbox_size, GFP_KERNEL);
36 	if (!ipc->rx.data)
37 		return -ENOMEM;
38 
39 	memcpy(&ipc->config, config, sizeof(*config));
40 	ipc->ready = true;
41 
42 	return 0;
43 }
44 
45 static void catpt_ipc_msg_init(struct catpt_ipc *ipc,
46 			       struct catpt_ipc_msg *reply)
47 {
48 	lockdep_assert_held(&ipc->lock);
49 
50 	ipc->rx.header = 0;
51 	ipc->rx.size = reply ? reply->size : 0;
52 	reinit_completion(&ipc->done_completion);
53 	reinit_completion(&ipc->busy_completion);
54 }
55 
56 static void catpt_dsp_send_tx(struct catpt_dev *cdev,
57 			      const struct catpt_ipc_msg *tx)
58 {
59 	u32 header = tx->header | CATPT_IPCC_BUSY;
60 
61 	trace_catpt_ipc_request(header);
62 	trace_catpt_ipc_payload(tx->data, tx->size);
63 
64 	memcpy_toio(catpt_outbox_addr(cdev), tx->data, tx->size);
65 	catpt_writel_shim(cdev, IPCC, header);
66 }
67 
68 static int catpt_wait_msg_completion(struct catpt_dev *cdev, int timeout)
69 {
70 	struct catpt_ipc *ipc = &cdev->ipc;
71 	int ret;
72 
73 	ret = wait_for_completion_timeout(&ipc->done_completion,
74 					  msecs_to_jiffies(timeout));
75 	if (!ret)
76 		return -ETIMEDOUT;
77 	if (ipc->rx.rsp.status != CATPT_REPLY_PENDING)
78 		return 0;
79 
80 	/* wait for delayed reply */
81 	ret = wait_for_completion_timeout(&ipc->busy_completion,
82 					  msecs_to_jiffies(timeout));
83 	return ret ? 0 : -ETIMEDOUT;
84 }
85 
86 static int catpt_dsp_do_send_msg(struct catpt_dev *cdev,
87 				 struct catpt_ipc_msg request,
88 				 struct catpt_ipc_msg *reply, int timeout, const char *name)
89 {
90 	struct catpt_ipc *ipc = &cdev->ipc;
91 	unsigned long flags;
92 	int ret;
93 
94 	if (!ipc->ready)
95 		return -EPERM;
96 	if (request.size > ipc->config.outbox_size ||
97 	    (reply && reply->size > ipc->config.outbox_size))
98 		return -EINVAL;
99 
100 	spin_lock_irqsave(&ipc->lock, flags);
101 	catpt_ipc_msg_init(ipc, reply);
102 	catpt_dsp_send_tx(cdev, &request);
103 	spin_unlock_irqrestore(&ipc->lock, flags);
104 
105 	ret = catpt_wait_msg_completion(cdev, timeout);
106 	if (ret) {
107 		dev_crit(cdev->dev, "communication severed: %d, rebooting dsp..\n",
108 			 ret);
109 		ipc->ready = false;
110 		/* TODO: attempt recovery */
111 		return ret;
112 	}
113 
114 	ret = ipc->rx.rsp.status;
115 	if (ret)
116 		dev_err(cdev->dev, "%s (0x%08x) failed: %d\n", name, request.header, ret);
117 	if (reply) {
118 		reply->header = ipc->rx.header;
119 
120 		if (!ret && reply->data)
121 			memcpy(reply->data, ipc->rx.data, reply->size);
122 	}
123 
124 	return ret;
125 }
126 
127 int catpt_dsp_send_msg_timeout(struct catpt_dev *cdev,
128 			       struct catpt_ipc_msg request,
129 			       struct catpt_ipc_msg *reply, int timeout, const char *name)
130 {
131 	struct catpt_ipc *ipc = &cdev->ipc;
132 	int ret;
133 
134 	mutex_lock(&ipc->mutex);
135 	ret = catpt_dsp_do_send_msg(cdev, request, reply, timeout, name);
136 	mutex_unlock(&ipc->mutex);
137 
138 	return ret;
139 }
140 
141 int catpt_dsp_send_msg(struct catpt_dev *cdev, struct catpt_ipc_msg request,
142 		       struct catpt_ipc_msg *reply, const char *name)
143 {
144 	return catpt_dsp_send_msg_timeout(cdev, request, reply,
145 					  cdev->ipc.default_timeout, name);
146 }
147 
148 static void
149 catpt_dsp_notify_stream(struct catpt_dev *cdev, union catpt_notify_msg msg)
150 {
151 	struct catpt_stream_runtime *stream;
152 	struct catpt_notify_position pos;
153 	struct catpt_notify_glitch glitch;
154 
155 	guard(mutex)(&cdev->stream_mutex);
156 
157 	stream = catpt_stream_find(cdev, msg.stream_hw_id);
158 	if (!stream) {
159 		dev_warn(cdev->dev, "notify %d for non-existent stream %d\n",
160 			 msg.notify_reason, msg.stream_hw_id);
161 		return;
162 	}
163 
164 	switch (msg.notify_reason) {
165 	case CATPT_NOTIFY_POSITION_CHANGED:
166 		memcpy_fromio(&pos, catpt_inbox_addr(cdev), sizeof(pos));
167 		trace_catpt_ipc_payload((u8 *)&pos, sizeof(pos));
168 
169 		catpt_stream_update_position(cdev, stream, &pos);
170 		break;
171 
172 	case CATPT_NOTIFY_GLITCH_OCCURRED:
173 		memcpy_fromio(&glitch, catpt_inbox_addr(cdev), sizeof(glitch));
174 		trace_catpt_ipc_payload((u8 *)&glitch, sizeof(glitch));
175 
176 		dev_warn(cdev->dev, "glitch %d at pos: 0x%08llx, wp: 0x%08x\n",
177 			 glitch.type, glitch.presentation_pos,
178 			 glitch.write_pos);
179 		break;
180 
181 	default:
182 		dev_warn(cdev->dev, "unknown notification: %d received\n",
183 			 msg.notify_reason);
184 		break;
185 	}
186 }
187 
188 static void catpt_dsp_copy_rx(struct catpt_dev *cdev, u32 header)
189 {
190 	struct catpt_ipc *ipc = &cdev->ipc;
191 
192 	ipc->rx.header = header;
193 	if (ipc->rx.rsp.status != CATPT_REPLY_SUCCESS)
194 		return;
195 
196 	memcpy_fromio(ipc->rx.data, catpt_outbox_addr(cdev), ipc->rx.size);
197 	trace_catpt_ipc_payload(ipc->rx.data, ipc->rx.size);
198 }
199 
200 static void catpt_dsp_process_response(struct catpt_dev *cdev, u32 header)
201 {
202 	union catpt_notify_msg msg = CATPT_MSG(header);
203 	struct catpt_ipc *ipc = &cdev->ipc;
204 
205 	if (msg.fw_ready) {
206 		struct catpt_fw_ready config;
207 		/* to fit 32b header original address is shifted right by 3 */
208 		u32 off = msg.mailbox_address << 3;
209 
210 		memcpy_fromio(&config, cdev->lpe_ba + off, sizeof(config));
211 		trace_catpt_ipc_payload((u8 *)&config, sizeof(config));
212 
213 		catpt_ipc_arm(ipc, &config);
214 		complete(&cdev->fw_ready);
215 		return;
216 	}
217 
218 	switch (msg.global_msg_type) {
219 	case CATPT_GLB_REQUEST_CORE_DUMP:
220 		dev_err(cdev->dev, "ADSP device coredump received\n");
221 		ipc->ready = false;
222 		catpt_coredump(cdev);
223 		/* TODO: attempt recovery */
224 		break;
225 
226 	case CATPT_GLB_STREAM_MESSAGE:
227 		switch (msg.stream_msg_type) {
228 		case CATPT_STRM_NOTIFICATION:
229 			catpt_dsp_notify_stream(cdev, msg);
230 			break;
231 		default:
232 			catpt_dsp_copy_rx(cdev, header);
233 			/* signal completion of delayed reply */
234 			complete(&ipc->busy_completion);
235 			break;
236 		}
237 		break;
238 
239 	default:
240 		dev_warn(cdev->dev, "unknown response: %d received\n",
241 			 msg.global_msg_type);
242 		break;
243 	}
244 }
245 
246 irqreturn_t catpt_dsp_irq_thread(int irq, void *dev_id)
247 {
248 	struct catpt_dev *cdev = dev_id;
249 	u32 ipcd;
250 
251 	ipcd = catpt_readl_shim(cdev, IPCD);
252 	trace_catpt_ipc_notify(ipcd);
253 
254 	/* ensure there is delayed reply or notification to process */
255 	if (!(ipcd & CATPT_IPCD_BUSY))
256 		return IRQ_NONE;
257 
258 	catpt_dsp_process_response(cdev, ipcd);
259 
260 	/* tell DSP processing is completed */
261 	catpt_updatel_shim(cdev, IPCD, CATPT_IPCD_BUSY | CATPT_IPCD_DONE,
262 			   CATPT_IPCD_DONE);
263 	/* unmask dsp BUSY interrupt */
264 	catpt_updatel_shim(cdev, IMC, CATPT_IMC_IPCDB, 0);
265 
266 	return IRQ_HANDLED;
267 }
268 
269 irqreturn_t catpt_dsp_irq_handler(int irq, void *dev_id)
270 {
271 	struct catpt_dev *cdev = dev_id;
272 	irqreturn_t ret = IRQ_NONE;
273 	u32 isc, ipcc;
274 
275 	isc = catpt_readl_shim(cdev, ISC);
276 	trace_catpt_irq(isc);
277 
278 	/* immediate reply */
279 	if (isc & CATPT_ISC_IPCCD) {
280 		/* mask host DONE interrupt */
281 		catpt_updatel_shim(cdev, IMC, CATPT_IMC_IPCCD, CATPT_IMC_IPCCD);
282 
283 		ipcc = catpt_readl_shim(cdev, IPCC);
284 		trace_catpt_ipc_reply(ipcc);
285 		catpt_dsp_copy_rx(cdev, ipcc);
286 		complete(&cdev->ipc.done_completion);
287 
288 		/* tell DSP processing is completed */
289 		catpt_updatel_shim(cdev, IPCC, CATPT_IPCC_DONE, 0);
290 		/* unmask host DONE interrupt */
291 		catpt_updatel_shim(cdev, IMC, CATPT_IMC_IPCCD, 0);
292 		ret = IRQ_HANDLED;
293 	}
294 
295 	/* delayed reply or notification */
296 	if (isc & CATPT_ISC_IPCDB) {
297 		/* mask dsp BUSY interrupt */
298 		catpt_updatel_shim(cdev, IMC, CATPT_IMC_IPCDB, CATPT_IMC_IPCDB);
299 		ret = IRQ_WAKE_THREAD;
300 	}
301 
302 	return ret;
303 }
304