aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/sound/soc/intel/catpt/ipc.c
blob: 5b718a846fda586c179816c9ad6943b6e48216bf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// SPDX-License-Identifier: GPL-2.0-only
//
// Copyright(c) 2020 Intel Corporation. All rights reserved.
//
// Author: Cezary Rojewski <cezary.rojewski@intel.com>
//

#include <linux/irqreturn.h>
#include "core.h"
#include "messages.h"
#include "registers.h"
#include "trace.h"

#define CATPT_IPC_TIMEOUT_MS	300

void catpt_ipc_init(struct catpt_ipc *ipc, struct device *dev)
{
	ipc->dev = dev;
	ipc->ready = false;
	ipc->default_timeout = CATPT_IPC_TIMEOUT_MS;
	init_completion(&ipc->done_completion);
	init_completion(&ipc->busy_completion);
	spin_lock_init(&ipc->lock);
	mutex_init(&ipc->mutex);
}

static int catpt_ipc_arm(struct catpt_ipc *ipc, struct catpt_fw_ready *config)
{
	/*
	 * Both tx and rx are put into and received from outbox. Inbox is
	 * only used for notifications where payload size is known upfront,
	 * thus no separate buffer is allocated for it.
	 */
	ipc->rx.data = devm_kzalloc(ipc->dev, config->outbox_size, GFP_KERNEL);
	if (!ipc->rx.data)
		return -ENOMEM;

	memcpy(&ipc->config, config, sizeof(*config));
	ipc->ready = true;

	return 0;
}

static void catpt_ipc_msg_init(struct catpt_ipc *ipc,
			       struct catpt_ipc_msg *reply)
{
	lockdep_assert_held(&ipc->lock);

	ipc->rx.header = 0;
	ipc->rx.size = reply ? reply->size : 0;
	reinit_completion(&ipc->done_completion);
	reinit_completion(&ipc->busy_completion);
}

static void catpt_dsp_send_tx(struct catpt_dev *cdev,
			      const struct catpt_ipc_msg *tx)
{
	u32 header = tx->header | CATPT_IPCC_BUSY;

	trace_catpt_ipc_request(header);
	trace_catpt_ipc_payload(tx->data, tx->size);

	memcpy_toio(catpt_outbox_addr(cdev), tx->data, tx->size);
	catpt_writel_shim(cdev, IPCC, header);
}

static int catpt_wait_msg_completion(struct catpt_dev *cdev, int timeout)
{
	struct catpt_ipc *ipc = &cdev->ipc;
	int ret;

	ret = wait_for_completion_timeout(&ipc->done_completion,
					  msecs_to_jiffies(timeout));
	if (!ret)
		return -ETIMEDOUT;
	if (ipc->rx.rsp.status != CATPT_REPLY_PENDING)
		return 0;

	/* wait for delayed reply */
	ret = wait_for_completion_timeout(&ipc->busy_completion,
					  msecs_to_jiffies(timeout));
	return ret ? 0 : -ETIMEDOUT;
}

static int catpt_dsp_do_send_msg(struct catpt_dev *cdev,
				 struct catpt_ipc_msg request,
				 struct catpt_ipc_msg *reply, int timeout)
{
	struct catpt_ipc *ipc = &cdev->ipc;
	unsigned long flags;
	int ret;

	if (!ipc->ready)
		return -EPERM;
	if (request.size > ipc->config.outbox_size ||
	    (reply && reply->size > ipc->config.outbox_size))
		return -EINVAL;

	spin_lock_irqsave(&ipc->lock, flags);
	catpt_ipc_msg_init(ipc, reply);
	catpt_dsp_send_tx(cdev, &request);
	spin_unlock_irqrestore(&ipc->lock, flags);

	ret = catpt_wait_msg_completion(cdev, timeout);
	if (ret) {
		dev_crit(cdev->dev, "communication severed: %d, rebooting dsp..\n",
			 ret);
		ipc->ready = false;
		/* TODO: attempt recovery */
		return ret;
	}

	ret = ipc->rx.rsp.status;
	if (reply) {
		reply->header = ipc->rx.header;

		if (!ret && reply->data)
			memcpy(reply->data, ipc->rx.data, reply->size);
	}

	return ret;
}

int catpt_dsp_send_msg_timeout(struct catpt_dev *cdev,
			       struct catpt_ipc_msg request,
			       struct catpt_ipc_msg *reply, int timeout)
{
	struct catpt_ipc *ipc = &cdev->ipc;
	int ret;

	mutex_lock(&ipc->mutex);
	ret = catpt_dsp_do_send_msg(cdev, request, reply, timeout);
	mutex_unlock(&ipc->mutex);

	return ret;
}

int catpt_dsp_send_msg(struct catpt_dev *cdev, struct catpt_ipc_msg request,
		       struct catpt_ipc_msg *reply)
{
	return catpt_dsp_send_msg_timeout(cdev, request, reply,
					  cdev->ipc.default_timeout);
}

static void
catpt_dsp_notify_stream(struct catpt_dev *cdev, union catpt_notify_msg msg)
{
	struct catpt_stream_runtime *stream;
	struct catpt_notify_position pos;
	struct catpt_notify_glitch glitch;

	stream = catpt_stream_find(cdev, msg.stream_hw_id);
	if (!stream) {
		dev_warn(cdev->dev, "notify %d for non-existent stream %d\n",
			 msg.notify_reason, msg.stream_hw_id);
		return;
	}

	switch (msg.notify_reason) {
	case CATPT_NOTIFY_POSITION_CHANGED:
		memcpy_fromio(&pos, catpt_inbox_addr(cdev), sizeof(pos));
		trace_catpt_ipc_payload((u8 *)&pos, sizeof(pos));

		catpt_stream_update_position(cdev, stream, &pos);
		break;

	case CATPT_NOTIFY_GLITCH_OCCURRED:
		memcpy_fromio(&glitch, catpt_inbox_addr(cdev), sizeof(glitch));
		trace_catpt_ipc_payload((u8 *)&glitch, sizeof(glitch));

		dev_warn(cdev->dev, "glitch %d at pos: 0x%08llx, wp: 0x%08x\n",
			 glitch.type, glitch.presentation_pos,
			 glitch.write_pos);
		break;

	default:
		dev_warn(cdev->dev, "unknown notification: %d received\n",
			 msg.notify_reason);
		break;
	}
}

static void catpt_dsp_copy_rx(struct catpt_dev *cdev, u32 header)
{
	struct catpt_ipc *ipc = &cdev->ipc;

	ipc->rx.header = header;
	if (ipc->rx.rsp.status != CATPT_REPLY_SUCCESS)
		return;

	memcpy_fromio(ipc->rx.data, catpt_outbox_addr(cdev), ipc->rx.size);
	trace_catpt_ipc_payload(ipc->rx.data, ipc->rx.size);
}

static void catpt_dsp_process_response(struct catpt_dev *cdev, u32 header)
{
	union catpt_notify_msg msg = CATPT_MSG(header);
	struct catpt_ipc *ipc = &cdev->ipc;

	if (msg.fw_ready) {
		struct catpt_fw_ready config;
		/* to fit 32b header original address is shifted right by 3 */
		u32 off = msg.mailbox_address << 3;

		memcpy_fromio(&config, cdev->lpe_ba + off, sizeof(config));
		trace_catpt_ipc_payload((u8 *)&config, sizeof(config));

		catpt_ipc_arm(ipc, &config);
		complete(&cdev->fw_ready);
		return;
	}

	switch (msg.global_msg_type) {
	case CATPT_GLB_REQUEST_CORE_DUMP:
		dev_err(cdev->dev, "ADSP device coredump received\n");
		ipc->ready = false;
		catpt_coredump(cdev);
		/* TODO: attempt recovery */
		break;

	case CATPT_GLB_STREAM_MESSAGE:
		switch (msg.stream_msg_type) {
		case CATPT_STRM_NOTIFICATION:
			catpt_dsp_notify_stream(cdev, msg);
			break;
		default:
			catpt_dsp_copy_rx(cdev, header);
			/* signal completion of delayed reply */
			complete(&ipc->busy_completion);
			break;
		}
		break;

	default:
		dev_warn(cdev->dev, "unknown response: %d received\n",
			 msg.global_msg_type);
		break;
	}
}

irqreturn_t catpt_dsp_irq_thread(int irq, void *dev_id)
{
	struct catpt_dev *cdev = dev_id;
	u32 ipcd;

	ipcd = catpt_readl_shim(cdev, IPCD);
	trace_catpt_ipc_notify(ipcd);

	/* ensure there is delayed reply or notification to process */
	if (!(ipcd & CATPT_IPCD_BUSY))
		return IRQ_NONE;

	catpt_dsp_process_response(cdev, ipcd);

	/* tell DSP processing is completed */
	catpt_updatel_shim(cdev, IPCD, CATPT_IPCD_BUSY | CATPT_IPCD_DONE,
			   CATPT_IPCD_DONE);
	/* unmask dsp BUSY interrupt */
	catpt_updatel_shim(cdev, IMC, CATPT_IMC_IPCDB, 0);

	return IRQ_HANDLED;
}

irqreturn_t catpt_dsp_irq_handler(int irq, void *dev_id)
{
	struct catpt_dev *cdev = dev_id;
	irqreturn_t ret = IRQ_NONE;
	u32 isc, ipcc;

	isc = catpt_readl_shim(cdev, ISC);
	trace_catpt_irq(isc);

	/* immediate reply */
	if (isc & CATPT_ISC_IPCCD) {
		/* mask host DONE interrupt */
		catpt_updatel_shim(cdev, IMC, CATPT_IMC_IPCCD, CATPT_IMC_IPCCD);

		ipcc = catpt_readl_shim(cdev, IPCC);
		trace_catpt_ipc_reply(ipcc);
		catpt_dsp_copy_rx(cdev, ipcc);
		complete(&cdev->ipc.done_completion);

		/* tell DSP processing is completed */
		catpt_updatel_shim(cdev, IPCC, CATPT_IPCC_DONE, 0);
		/* unmask host DONE interrupt */
		catpt_updatel_shim(cdev, IMC, CATPT_IMC_IPCCD, 0);
		ret = IRQ_HANDLED;
	}

	/* delayed reply or notification */
	if (isc & CATPT_ISC_IPCDB) {
		/* mask dsp BUSY interrupt */
		catpt_updatel_shim(cdev, IMC, CATPT_IMC_IPCDB, CATPT_IMC_IPCDB);
		ret = IRQ_WAKE_THREAD;
	}

	return ret;
}