summaryrefslogtreecommitdiff
path: root/drivers/mxc/vpu-encoder-b0/vpu_event_msg.c
blob: 4768dc7d1dcb041f458b39bc87133f66f4c70b44 (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
/*
 * Copyright(c) 2018 NXP. All rights reserved.
 *
 * This file is provided under a dual BSD/GPLv2 license.  When using or
 * redistributing this file, you may do so under either license.
 *
 * vpu_event_msg.c
 *
 * Author Ming Qian<ming.qian@nxp.com>
 */
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/vmalloc.h>

#include "vpu_encoder_b0.h"
#include "vpu_event_msg.h"

static atomic64_t total_ext_data = ATOMIC64_INIT(0);

static struct vpu_event_msg *alloc_event_msg(void)
{
	struct vpu_event_msg *msg = NULL;

	msg = vzalloc(sizeof(*msg));

	return msg;
}

static void free_event_msg(struct vpu_event_msg *msg)
{
	if (!msg)
		return;

	free_msg_ext_buffer(msg);
	vfree(msg);
}

static void set_msg_count(struct vpu_ctx *ctx, unsigned long count)
{
	struct vpu_attr *attr = get_vpu_ctx_attr(ctx);

	if (attr)
		attr->msg_count = count;
}

static void inc_msg_count(struct vpu_ctx *ctx)
{
	struct vpu_attr *attr = get_vpu_ctx_attr(ctx);

	if (attr)
		attr->msg_count++;
}

static void dec_msg_count(struct vpu_ctx *ctx)
{
	struct vpu_attr *attr = get_vpu_ctx_attr(ctx);

	if (attr)
		attr->msg_count--;
}

static bool is_msg_count_full(struct vpu_ctx *ctx)
{
	struct vpu_attr *attr = get_vpu_ctx_attr(ctx);

	if (!attr)
		return false;
	if (attr->msg_count > MSG_COUNT_THD)
		return true;
	return false;
}

void cleanup_ctx_msg_queue(struct vpu_ctx *ctx)
{
	struct vpu_event_msg *msg;
	struct vpu_event_msg *tmp;

	WARN_ON(!ctx);

	mutex_lock(&ctx->instance_mutex);
	list_for_each_entry_safe(msg, tmp, &ctx->msg_q, list) {
		list_del_init(&msg->list);
		vpu_dbg(LVL_WARN, "drop core[%d] ctx[%d] msg:[%d]\n",
				ctx->core_dev->id, ctx->str_index, msg->msgid);
		free_event_msg(msg);
		dec_msg_count(ctx);
	}

	list_for_each_entry_safe(msg, tmp, &ctx->idle_q, list) {
		list_del_init(&msg->list);
		free_event_msg(msg);
		dec_msg_count(ctx);
	}
	mutex_unlock(&ctx->instance_mutex);
}

static int increase_idle_msg(struct vpu_ctx *ctx, u32 count)
{
	int i;

	for (i = 0; i < count; i++) {
		struct vpu_event_msg *msg = alloc_event_msg();

		if (!msg)
			continue;
		list_add_tail(&msg->list, &ctx->idle_q);
		inc_msg_count(ctx);
	}

	return 0;
}

int init_ctx_msg_queue(struct vpu_ctx *ctx)
{
	WARN_ON(!ctx);
	if (!ctx)
		return -EINVAL;

	mutex_lock(&ctx->instance_mutex);

	set_msg_count(ctx, 0);
	INIT_LIST_HEAD(&ctx->msg_q);
	INIT_LIST_HEAD(&ctx->idle_q);

	mutex_unlock(&ctx->instance_mutex);

	return 0;
}

struct vpu_event_msg *get_idle_msg(struct vpu_ctx *ctx)
{
	struct vpu_event_msg *msg = NULL;

	WARN_ON(!ctx);

	mutex_lock(&ctx->instance_mutex);
	if (list_empty(&ctx->idle_q))
		increase_idle_msg(ctx, 1);

	msg = list_first_entry(&ctx->idle_q, struct vpu_event_msg, list);
	if (msg)
		list_del_init(&msg->list);

	mutex_unlock(&ctx->instance_mutex);

	return msg;
}

void put_idle_msg(struct vpu_ctx *ctx, struct vpu_event_msg *msg)
{
	WARN_ON(!ctx);

	if (!ctx || !msg)
		return;

	free_msg_ext_buffer(msg);

	mutex_lock(&ctx->instance_mutex);
	if (is_msg_count_full(ctx)) {
		free_event_msg(msg);
		dec_msg_count(ctx);
	} else {
		list_add_tail(&msg->list, &ctx->idle_q);
	}
	mutex_unlock(&ctx->instance_mutex);
}

struct vpu_event_msg *pop_event_msg(struct vpu_ctx *ctx)
{
	struct vpu_event_msg *msg = NULL;

	WARN_ON(!ctx);

	mutex_lock(&ctx->instance_mutex);
	if (list_empty(&ctx->msg_q))
		goto exit;

	msg = list_first_entry(&ctx->msg_q, struct vpu_event_msg, list);
	if (msg)
		list_del_init(&msg->list);

exit:
	mutex_unlock(&ctx->instance_mutex);
	return msg;
}

void push_back_event_msg(struct vpu_ctx *ctx, struct vpu_event_msg *msg)
{
	WARN_ON(!ctx);

	if (!ctx || !msg)
		return;

	mutex_lock(&ctx->instance_mutex);
	list_add_tail(&msg->list, &ctx->msg_q);
	mutex_unlock(&ctx->instance_mutex);
}

int alloc_msg_ext_buffer(struct vpu_event_msg *msg, u32 number)
{
	WARN_ON(!msg);

	if (!msg || !number)
		return -EINVAL;

	msg->ext_data = vzalloc(number * sizeof(u32));
	if (!msg->ext_data)
		return -ENOMEM;
	msg->number = number;

	atomic64_add(number, &total_ext_data);
	vpu_dbg(LVL_DEBUG, "++++alloc %d msg ext data: %lld\n",
			number, get_total_ext_data_number());

	return 0;
}

void free_msg_ext_buffer(struct vpu_event_msg *msg)
{
	WARN_ON(!msg);

	if (!msg || !msg->ext_data)
		return;

	atomic64_sub(msg->number, &total_ext_data);
	vfree(msg->ext_data);
	msg->ext_data = NULL;
	vpu_dbg(LVL_DEBUG, "----free %d msg ext data: %lld\n",
			msg->number, get_total_ext_data_number());
}

long long get_total_ext_data_number(void)
{
	return atomic64_read(&total_ext_data);
}