summaryrefslogtreecommitdiff
path: root/drivers/soc/qcom/smp2p_loopback.c
blob: c840eea94bfaa6404e10498650bce2eb9577c94c (plain)
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/* drivers/soc/qcom/smp2p_loopback.c
 *
 * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
#include <linux/debugfs.h>
#include <linux/list.h>
#include <linux/ctype.h>
#include <linux/jiffies.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/completion.h>
#include <linux/termios.h>
#include <linux/module.h>
#include <linux/remote_spinlock.h>
#include "smem_private.h"
#include "smp2p_private.h"

/**
 * struct smp2p_loopback_ctx - Representation of remote loopback object.
 *
 * @proc_id: Processor id of the processor that sends the loopback commands.
 * @out: Handle to the  smem entry structure for providing the response.
 * @out_nb: Notifies the opening of local entry.
 * @out_is_active: Outbound entry events should be processed.
 * @in_nb: Notifies changes in the remote entry.
 * @in_is_active: Inbound entry events should be processed.
 * @rmt_lpb_work: Work item that handles the incoming loopback commands.
 * @rmt_cmd: Structure that holds the current and previous value of the entry.
 */
struct smp2p_loopback_ctx {
	int proc_id;
	struct msm_smp2p_out *out;
	struct notifier_block out_nb;
	bool out_is_active;
	struct notifier_block in_nb;
	bool in_is_active;
	struct work_struct  rmt_lpb_work;
	struct msm_smp2p_update_notif rmt_cmd;
};

static struct smp2p_loopback_ctx  remote_loopback[SMP2P_NUM_PROCS];
static struct msm_smp2p_remote_mock remote_mock;

/**
 * remote_spinlock_test - Handles remote spinlock test.
 *
 * @ctx: Loopback context
 */
static void remote_spinlock_test(struct smp2p_loopback_ctx *ctx)
{
	uint32_t test_request;
	uint32_t test_response;
	unsigned long flags;
	int n;
	unsigned lock_count = 0;
	remote_spinlock_t *smem_spinlock;

	test_request = 0x0;
	SMP2P_SET_RMT_CMD_TYPE_REQ(test_request);
	smem_spinlock = smem_get_remote_spinlock();
	if (!smem_spinlock) {
		pr_err("%s: unable to get remote spinlock\n", __func__);
		return;
	}

	for (;;) {
		remote_spin_lock_irqsave(smem_spinlock, flags);
		++lock_count;
		SMP2P_SET_RMT_CMD(test_request, SMP2P_LB_CMD_RSPIN_LOCKED);
		(void)msm_smp2p_out_write(ctx->out, test_request);

		for (n = 0; n < 10000; ++n) {
			(void)msm_smp2p_in_read(ctx->proc_id,
					"smp2p", &test_response);
			test_response = SMP2P_GET_RMT_CMD(test_response);

			if (test_response == SMP2P_LB_CMD_RSPIN_END)
				break;

			if (test_response != SMP2P_LB_CMD_RSPIN_UNLOCKED)
				SMP2P_ERR("%s: invalid spinlock command %x\n",
					__func__, test_response);
		}

		if (test_response == SMP2P_LB_CMD_RSPIN_END) {
			SMP2P_SET_RMT_CMD_TYPE_RESP(test_request);
			SMP2P_SET_RMT_CMD(test_request,
					SMP2P_LB_CMD_RSPIN_END);
			SMP2P_SET_RMT_DATA(test_request, lock_count);
			(void)msm_smp2p_out_write(ctx->out, test_request);
			break;
		}

		SMP2P_SET_RMT_CMD(test_request, SMP2P_LB_CMD_RSPIN_UNLOCKED);
		(void)msm_smp2p_out_write(ctx->out, test_request);
		remote_spin_unlock_irqrestore(smem_spinlock, flags);
	}
	remote_spin_unlock_irqrestore(smem_spinlock, flags);
}

/**
 * smp2p_rmt_lpb_worker - Handles incoming remote loopback commands.
 *
 * @work: Work Item scheduled to handle the incoming commands.
 */
static void smp2p_rmt_lpb_worker(struct work_struct *work)
{
	struct smp2p_loopback_ctx *ctx;
	int lpb_cmd;
	int lpb_cmd_type;
	int lpb_data;

	ctx = container_of(work, struct smp2p_loopback_ctx, rmt_lpb_work);

	if (!ctx->in_is_active || !ctx->out_is_active)
		return;

	if (ctx->rmt_cmd.previous_value == ctx->rmt_cmd.current_value)
		return;

	lpb_cmd_type =  SMP2P_GET_RMT_CMD_TYPE(ctx->rmt_cmd.current_value);
	lpb_cmd = SMP2P_GET_RMT_CMD(ctx->rmt_cmd.current_value);
	lpb_data = SMP2P_GET_RMT_DATA(ctx->rmt_cmd.current_value);

	if (lpb_cmd & SMP2P_RLPB_IGNORE)
		return;

	switch (lpb_cmd) {
	case SMP2P_LB_CMD_NOOP:
	    /* Do nothing */
	    break;

	case SMP2P_LB_CMD_ECHO:
		SMP2P_SET_RMT_CMD_TYPE(ctx->rmt_cmd.current_value, 0);
		SMP2P_SET_RMT_DATA(ctx->rmt_cmd.current_value,
							lpb_data);
		(void)msm_smp2p_out_write(ctx->out,
					ctx->rmt_cmd.current_value);
	    break;

	case SMP2P_LB_CMD_CLEARALL:
		ctx->rmt_cmd.current_value = 0;
		(void)msm_smp2p_out_write(ctx->out,
					ctx->rmt_cmd.current_value);
	    break;

	case SMP2P_LB_CMD_PINGPONG:
		SMP2P_SET_RMT_CMD_TYPE(ctx->rmt_cmd.current_value, 0);
		if (lpb_data) {
			lpb_data--;
			SMP2P_SET_RMT_DATA(ctx->rmt_cmd.current_value,
					lpb_data);
			(void)msm_smp2p_out_write(ctx->out,
					ctx->rmt_cmd.current_value);
		}
	    break;

	case SMP2P_LB_CMD_RSPIN_START:
		remote_spinlock_test(ctx);
		break;

	case SMP2P_LB_CMD_RSPIN_LOCKED:
	case SMP2P_LB_CMD_RSPIN_UNLOCKED:
	case SMP2P_LB_CMD_RSPIN_END:
		/* not used for remote spinlock test */
		break;

	default:
		SMP2P_DBG("%s: Unknown loopback command %x\n",
				__func__, lpb_cmd);
		break;
	}
}

/**
 * smp2p_rmt_in_edge_notify -  Schedules a work item to handle the commands.
 *
 * @nb: Notifier block, this is called when the value in remote entry changes.
 * @event: Takes value SMP2P_ENTRY_UPDATE or SMP2P_OPEN based on the event.
 * @data: Consists of previous and current value in case of entry update.
 * @returns: 0 for success (return value required for notifier chains).
 */
static int smp2p_rmt_in_edge_notify(struct notifier_block *nb,
				unsigned long event, void *data)
{
	struct smp2p_loopback_ctx *ctx;

	if (!(event == SMP2P_ENTRY_UPDATE || event == SMP2P_OPEN))
		return 0;

	ctx = container_of(nb, struct smp2p_loopback_ctx, in_nb);
	if (data && ctx->in_is_active) {
			ctx->rmt_cmd =
			    *(struct msm_smp2p_update_notif *)data;
			schedule_work(&ctx->rmt_lpb_work);
	}

	return 0;
}

/**
 * smp2p_rmt_out_edge_notify - Notifies on the opening of the outbound entry.
 *
 * @nb: Notifier block, this is called when the local entry is open.
 * @event: Takes on value SMP2P_OPEN when the local entry is open.
 * @data: Consist of current value of the remote entry, if entry is open.
 * @returns: 0 for success (return value required for notifier chains).
 */
static int smp2p_rmt_out_edge_notify(struct notifier_block  *nb,
				unsigned long event, void *data)
{
	struct smp2p_loopback_ctx *ctx;

	ctx = container_of(nb, struct smp2p_loopback_ctx, out_nb);
	if (event == SMP2P_OPEN)
		SMP2P_DBG("%s: 'smp2p':%d opened\n", __func__,
				ctx->proc_id);

	return 0;
}

/**
 * msm_smp2p_init_rmt_lpb -  Initializes the remote loopback object.
 *
 * @ctx: Pointer to remote loopback object that needs to be initialized.
 * @pid: Processor id  of the processor that is sending the commands.
 * @entry: Name of the entry that needs to be opened locally.
 * @returns: 0 on success, standard Linux error code otherwise.
 */
static int msm_smp2p_init_rmt_lpb(struct  smp2p_loopback_ctx *ctx,
			int pid, const char *entry)
{
	int ret = 0;
	int tmp;

	if (!ctx || !entry || pid > SMP2P_NUM_PROCS)
		return -EINVAL;

	ctx->in_nb.notifier_call = smp2p_rmt_in_edge_notify;
	ctx->out_nb.notifier_call = smp2p_rmt_out_edge_notify;
	ctx->proc_id = pid;
	ctx->in_is_active = true;
	ctx->out_is_active = true;
	tmp = msm_smp2p_out_open(pid, entry, &ctx->out_nb,
						&ctx->out);
	if (tmp) {
		SMP2P_ERR("%s: open failed outbound entry '%s':%d - ret %d\n",
				__func__, entry, pid, tmp);
		ret = tmp;
	}

	tmp = msm_smp2p_in_register(ctx->proc_id,
				SMP2P_RLPB_ENTRY_NAME,
				&ctx->in_nb);
	if (tmp) {
		SMP2P_ERR("%s: unable to open inbound entry '%s':%d - ret %d\n",
				__func__, entry, pid, tmp);
		ret = tmp;
	}

	return ret;
}

/**
 * msm_smp2p_init_rmt_lpb_proc - Wrapper over msm_smp2p_init_rmt_lpb
 *
 * @remote_pid: Processor ID of the processor that sends loopback command.
 * @returns: Pointer to outbound entry handle.
 */
void *msm_smp2p_init_rmt_lpb_proc(int remote_pid)
{
	int tmp;
	void *ret = NULL;

	tmp = msm_smp2p_init_rmt_lpb(&remote_loopback[remote_pid],
			remote_pid, SMP2P_RLPB_ENTRY_NAME);
	if (!tmp)
		ret = remote_loopback[remote_pid].out;

	return ret;
}
EXPORT_SYMBOL(msm_smp2p_init_rmt_lpb_proc);

/**
 * msm_smp2p_deinit_rmt_lpb_proc - Unregister support for remote processor.
 *
 * @remote_pid:  Processor ID of the remote system.
 * @returns: 0 on success, standard Linux error code otherwise.
 *
 * Unregister loopback support for remote processor.
 */
int msm_smp2p_deinit_rmt_lpb_proc(int remote_pid)
{
	int ret = 0;
	int tmp;
	struct smp2p_loopback_ctx *ctx;

	if (remote_pid >= SMP2P_NUM_PROCS)
		return -EINVAL;

	ctx = &remote_loopback[remote_pid];

	/* abort any pending notifications */
	remote_loopback[remote_pid].out_is_active = false;
	remote_loopback[remote_pid].in_is_active = false;
	flush_work(&ctx->rmt_lpb_work);

	/* unregister entries */
	tmp = msm_smp2p_out_close(&remote_loopback[remote_pid].out);
	remote_loopback[remote_pid].out = NULL;
	if (tmp) {
		SMP2P_ERR("%s: outbound 'smp2p':%d close failed %d\n",
				__func__, remote_pid, tmp);
		ret = tmp;
	}

	tmp = msm_smp2p_in_unregister(remote_pid,
		SMP2P_RLPB_ENTRY_NAME, &remote_loopback[remote_pid].in_nb);
	if (tmp) {
		SMP2P_ERR("%s: inbound 'smp2p':%d close failed %d\n",
				__func__, remote_pid, tmp);
		ret = tmp;
	}

	return ret;
}
EXPORT_SYMBOL(msm_smp2p_deinit_rmt_lpb_proc);

/**
 * msm_smp2p_set_remote_mock_exists - Sets the remote mock configuration.
 *
 * @item_exists: true = Remote mock SMEM item exists
 *
 * This is used in the testing environment to simulate the existence of the
 * remote smem item in order to test the negotiation algorithm.
 */
void msm_smp2p_set_remote_mock_exists(bool item_exists)
{
	remote_mock.item_exists = item_exists;
}
EXPORT_SYMBOL(msm_smp2p_set_remote_mock_exists);

/**
 * msm_smp2p_get_remote_mock - Get remote mock object.
 *
 * @returns: Point to the remote mock object.
 */
void *msm_smp2p_get_remote_mock(void)
{
	return &remote_mock;
}
EXPORT_SYMBOL(msm_smp2p_get_remote_mock);

/**
 * msm_smp2p_get_remote_mock_smem_item - Returns a pointer to remote item.
 *
 * @size:    Size of item.
 * @returns: Pointer to mock remote smem item.
 */
void *msm_smp2p_get_remote_mock_smem_item(uint32_t *size)
{
	void *ptr = NULL;
	if (remote_mock.item_exists) {
		*size = sizeof(remote_mock.remote_item);
		ptr = &(remote_mock.remote_item);
	}

	return ptr;
}
EXPORT_SYMBOL(msm_smp2p_get_remote_mock_smem_item);

/**
 * smp2p_remote_mock_rx_interrupt - Triggers receive interrupt for mock proc.
 *
 * @returns: 0 for success
 *
 * This function simulates the receiving of interrupt by the mock remote
 * processor in a testing environment.
 */
int smp2p_remote_mock_rx_interrupt(void)
{
	remote_mock.rx_interrupt_count++;
	if (remote_mock.initialized)
		complete(&remote_mock.cb_completion);
	return 0;
}
EXPORT_SYMBOL(smp2p_remote_mock_rx_interrupt);

/**
 * smp2p_remote_mock_tx_interrupt - Calls the SMP2P interrupt handler.
 *
 * This function calls the interrupt handler of the Apps processor to simulate
 * receiving interrupts from a remote processor.
 */
static void smp2p_remote_mock_tx_interrupt(void)
{
	msm_smp2p_interrupt_handler(SMP2P_REMOTE_MOCK_PROC);
}

/**
 * smp2p_remote_mock_init - Initialize the remote mock and loopback objects.
 *
 * @returns: 0 for success
 */
static int __init smp2p_remote_mock_init(void)
{
	int i;
	struct smp2p_interrupt_config *int_cfg;

	smp2p_init_header(&remote_mock.remote_item.header,
			SMP2P_REMOTE_MOCK_PROC, SMP2P_APPS_PROC,
			0, 0);
	remote_mock.rx_interrupt_count = 0;
	remote_mock.rx_interrupt = smp2p_remote_mock_rx_interrupt;
	remote_mock.tx_interrupt = smp2p_remote_mock_tx_interrupt;
	remote_mock.item_exists = false;
	init_completion(&remote_mock.cb_completion);
	remote_mock.initialized = true;

	for (i = 0; i < SMP2P_NUM_PROCS; i++) {
		INIT_WORK(&(remote_loopback[i].rmt_lpb_work),
				smp2p_rmt_lpb_worker);
		if (i == SMP2P_REMOTE_MOCK_PROC)
			/* do not register loopback for remote mock proc */
			continue;

		int_cfg = smp2p_get_interrupt_config();
		if (!int_cfg) {
			SMP2P_ERR("Remote processor config unavailable\n");
			return 0;
		}
		if (!int_cfg[i].is_configured)
			continue;

		msm_smp2p_init_rmt_lpb(&remote_loopback[i],
			i, SMP2P_RLPB_ENTRY_NAME);
	}
	return 0;
}
module_init(smp2p_remote_mock_init);