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