summaryrefslogtreecommitdiff
path: root/mali_kbase/tests/kutf/kutf_resultset.c
blob: 41645a4af46dcc0e6dfd60df69e0099ccb0621be (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
/*
 *
 * (C) COPYRIGHT 2014, 2017 ARM Limited. All rights reserved.
 *
 * This program is free software and is provided to you under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation, and any use by you of this program is subject to the terms
 * of such GNU licence.
 *
 * A copy of the licence is included with the program, and can also be obtained
 * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 *
 */



/* Kernel UTF result management functions */

#include <linux/list.h>
#include <linux/slab.h>
#include <linux/printk.h>
#include <linux/sched.h>
#include <linux/wait.h>
#include <linux/err.h>

#include <kutf/kutf_suite.h>
#include <kutf/kutf_resultset.h>

/* Lock to protect all result structures */
static DEFINE_SPINLOCK(kutf_result_lock);

struct kutf_result_set *kutf_create_result_set(void)
{
	struct kutf_result_set *set;

	set = kmalloc(sizeof(*set), GFP_KERNEL);
	if (!set) {
		pr_err("Failed to allocate resultset");
		goto fail_alloc;
	}

	INIT_LIST_HEAD(&set->results);
	init_waitqueue_head(&set->waitq);
	set->flags = 0;

	return set;

fail_alloc:
	return NULL;
}

int kutf_add_result(struct kutf_context *context,
		enum kutf_result_status status,
		const char *message)
{
	struct kutf_mempool *mempool = &context->fixture_pool;
	struct kutf_result_set *set = context->result_set;
	/* Create the new result */
	struct kutf_result *new_result;

	BUG_ON(set == NULL);

	new_result = kutf_mempool_alloc(mempool, sizeof(*new_result));
	if (!new_result) {
		pr_err("Result allocation failed\n");
		return -ENOMEM;
	}

	INIT_LIST_HEAD(&new_result->node);
	new_result->status = status;
	new_result->message = message;

	spin_lock(&kutf_result_lock);

	list_add_tail(&new_result->node, &set->results);

	spin_unlock(&kutf_result_lock);

	wake_up(&set->waitq);

	return 0;
}

void kutf_destroy_result_set(struct kutf_result_set *set)
{
	if (!list_empty(&set->results))
		pr_err("kutf_destroy_result_set: Unread results from test\n");

	kfree(set);
}

static bool kutf_has_result(struct kutf_result_set *set)
{
	bool has_result;

	spin_lock(&kutf_result_lock);
	if (set->flags & KUTF_RESULT_SET_WAITING_FOR_INPUT)
		/* Pretend there are results if waiting for input */
		has_result = true;
	else
		has_result = !list_empty(&set->results);
	spin_unlock(&kutf_result_lock);

	return has_result;
}

struct kutf_result *kutf_remove_result(struct kutf_result_set *set)
{
	struct kutf_result *result = NULL;
	int ret;

	do {
		ret = wait_event_interruptible(set->waitq,
				kutf_has_result(set));

		if (ret)
			return ERR_PTR(ret);

		spin_lock(&kutf_result_lock);

		if (!list_empty(&set->results)) {
			result = list_first_entry(&set->results,
					struct kutf_result,
					node);
			list_del(&result->node);
		} else if (set->flags & KUTF_RESULT_SET_WAITING_FOR_INPUT) {
			/* Return a fake result */
			static struct kutf_result waiting = {
				.status = KUTF_RESULT_USERDATA_WAIT
			};
			result = &waiting;
		}
		/* If result == NULL then there was a race with the event
		 * being removed between the check in kutf_has_result and
		 * the lock being obtained. In this case we retry
		 */

		spin_unlock(&kutf_result_lock);
	} while (result == NULL);

	return result;
}

void kutf_set_waiting_for_input(struct kutf_result_set *set)
{
	spin_lock(&kutf_result_lock);
	set->flags |= KUTF_RESULT_SET_WAITING_FOR_INPUT;
	spin_unlock(&kutf_result_lock);

	wake_up(&set->waitq);
}

void kutf_clear_waiting_for_input(struct kutf_result_set *set)
{
	spin_lock(&kutf_result_lock);
	set->flags &= ~KUTF_RESULT_SET_WAITING_FOR_INPUT;
	spin_unlock(&kutf_result_lock);
}