summaryrefslogtreecommitdiff
path: root/drivers/soc/qcom/smp2p_test_common.h
blob: 3be519bc0c96f3daf8efde3735b95ffbe1c822c1 (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
/* drivers/soc/qcom/smp2p_test_common.h
 *
 * Copyright (c) 2013-2014,2016 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.
 */
#ifndef _SMP2P_TEST_COMMON_H_
#define _SMP2P_TEST_COMMON_H_

#include <linux/debugfs.h>

/**
 * Unit test assertion for logging test cases.
 *
 * @a lval
 * @b rval
 * @cmp comparison operator
 *
 * Assertion fails if (@a cmp @b) is not true which then
 * logs the function and line number where the error occurred
 * along with the values of @a and @b.
 *
 * Assumes that the following local variables exist:
 * @s - sequential output file pointer
 * @failed - set to true if test fails
 */
#define UT_ASSERT_INT(a, cmp, b) \
	{ \
	int a_tmp = (a); \
	int b_tmp = (b); \
	if (!((a_tmp)cmp(b_tmp))) { \
		seq_printf(s, "%s:%d Fail: " #a "(%d) " #cmp " " #b "(%d)\n", \
				__func__, __LINE__, \
				a_tmp, b_tmp); \
		failed = 1; \
		break; \
	} \
	}

#define UT_ASSERT_PTR(a, cmp, b) \
	{ \
	void *a_tmp = (a); \
	void *b_tmp = (b); \
	if (!((a_tmp)cmp(b_tmp))) { \
		seq_printf(s, "%s:%d Fail: " #a "(%pK) " #cmp \
				" " #b "(%pK)\n", \
				__func__, __LINE__, \
				a_tmp, b_tmp); \
		failed = 1; \
		break; \
	} \
	}

#define UT_ASSERT_UINT(a, cmp, b) \
	{ \
	unsigned a_tmp = (a); \
	unsigned b_tmp = (b); \
	if (!((a_tmp)cmp(b_tmp))) { \
		seq_printf(s, "%s:%d Fail: " #a "(%u) " #cmp " " #b "(%u)\n", \
				__func__, __LINE__, \
				a_tmp, b_tmp); \
		failed = 1; \
		break; \
	} \
	}

#define UT_ASSERT_HEX(a, cmp, b) \
	{ \
	unsigned a_tmp = (a); \
	unsigned b_tmp = (b); \
	if (!((a_tmp)cmp(b_tmp))) { \
		seq_printf(s, "%s:%d Fail: " #a "(%x) " #cmp " " #b "(%x)\n", \
				__func__, __LINE__, \
				a_tmp, b_tmp); \
		failed = 1; \
		break; \
	} \
	}

/**
 * In-range unit test assertion for test cases.
 *
 * @a lval
 * @minv Minimum value
 * @maxv Maximum value
 *
 * Assertion fails if @a is not on the exclusive range minv, maxv
 * ((@a < @minv) or (@a > @maxv)).  In the failure case, the macro
 * logs the function and line number where the error occurred along
 * with the values of @a and @minv, @maxv.
 *
 * Assumes that the following local variables exist:
 * @s - sequential output file pointer
 * @failed - set to true if test fails
 */
#define UT_ASSERT_INT_IN_RANGE(a, minv, maxv) \
	{ \
	int a_tmp = (a); \
	int minv_tmp = (minv); \
	int maxv_tmp = (maxv); \
	if (((a_tmp) < (minv_tmp)) || ((a_tmp) > (maxv_tmp))) { \
		seq_printf(s, "%s:%d Fail: " #a "(%d) < " #minv "(%d) or " \
				 #a "(%d) > " #maxv "(%d)\n", \
				__func__, __LINE__, \
				a_tmp, minv_tmp, a_tmp, maxv_tmp); \
		failed = 1; \
		break; \
	} \
	}

/* Structure to track state changes for the notifier callback. */
struct mock_cb_data {
	bool initialized;
	spinlock_t lock;
	struct notifier_block nb;

	/* events */
	struct completion cb_completion;
	int cb_count;
	int event_open;
	int event_entry_update;
	struct msm_smp2p_update_notif entry_data;
};

void smp2p_debug_create(const char *name, void (*show)(struct seq_file *));
void smp2p_debug_create_u32(const char *name, uint32_t *value);
static inline int smp2p_test_notify(struct notifier_block *self,
	unsigned long event, void *data);

/**
 * Reset mock callback data to default values.
 *
 * @cb:  Mock callback data
 */
static inline void mock_cb_data_reset(struct mock_cb_data *cb)
{
	reinit_completion(&cb->cb_completion);
	cb->cb_count = 0;
	cb->event_open = 0;
	cb->event_entry_update = 0;
	memset(&cb->entry_data, 0,
		sizeof(struct msm_smp2p_update_notif));
}


/**
 * Initialize mock callback data.
 *
 * @cb:  Mock callback data
 */
static inline void mock_cb_data_init(struct mock_cb_data *cb)
{
	if (!cb->initialized) {
		init_completion(&cb->cb_completion);
		spin_lock_init(&cb->lock);
		cb->initialized = true;
		cb->nb.notifier_call = smp2p_test_notify;
		memset(&cb->entry_data, 0,
			sizeof(struct msm_smp2p_update_notif));
	}
	mock_cb_data_reset(cb);
}

/**
 * Notifier function passed into SMP2P for testing.
 *
 * @self:       Pointer to calling notifier block
 * @event:	    Event
 * @data:       Event-specific data
 * @returns:    0
 */
static inline int smp2p_test_notify(struct notifier_block *self,
		unsigned long event, void *data)
{
	struct mock_cb_data *cb_data_ptr;
	unsigned long flags;

	cb_data_ptr = container_of(self, struct mock_cb_data, nb);

	spin_lock_irqsave(&cb_data_ptr->lock, flags);

	switch (event) {
	case SMP2P_OPEN:
		++cb_data_ptr->event_open;
		if (data) {
			cb_data_ptr->entry_data =
			*(struct msm_smp2p_update_notif *)(data);
		}
		break;
	case SMP2P_ENTRY_UPDATE:
		++cb_data_ptr->event_entry_update;
		if (data) {
			cb_data_ptr->entry_data =
			*(struct msm_smp2p_update_notif *)(data);
		}
		break;
	default:
		pr_err("%s Unknown event\n", __func__);
		break;
	}

	++cb_data_ptr->cb_count;
	complete(&cb_data_ptr->cb_completion);
	spin_unlock_irqrestore(&cb_data_ptr->lock, flags);
	return 0;
}
#endif /* _SMP2P_TEST_COMMON_H_ */