summaryrefstree
path: root/kernel/drivers/vision/vpu/interface/hardware/vpu-mailbox.c
blob: c675517a8863a2b1c3d34a7c73ddc2bc3f15bb8b (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
/*
 * Samsung Exynos SoC series VPU driver
 *
 * Copyright (c) 2015 Samsung Electronics Co., Ltd
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/delay.h>
#include <linux/random.h>
#include <linux/slab.h>
#include <linux/kernel.h>

#include "vpu-config.h"
#include "vpu-io.h"
#include "vpu-mailbox.h"
#include "vpu-interface.h"

struct vpu_mailbox_hdr * vpu_mbox_g_hdr(void *code, u32 code_size)
{
	return (struct vpu_mailbox_hdr *)((char *)code + code_size - sizeof(struct vpu_mailbox_hdr));
}

struct vpu_mailbox_stack * vpu_mbox_g_stack(struct vpu_mailbox_hdr *hdr)
{
	return (struct vpu_mailbox_stack *)((char *)hdr + sizeof(struct vpu_mailbox_hdr)
		- VPU_MAILBOX_BASEOFFSET * 2 - (hdr->stack_ofs16 * 2));
}

inline u16 * vpu_h2f_g_mboxdata(struct vpu_mailbox_h2f *mbox)
{
	return ((u16 *)mbox) + mbox->data_ofs16;
}

inline u16 * vpu_f2h_g_mboxdata(struct vpu_mailbox_f2h *mbox)
{
	return ((u16 *)mbox) + mbox->data_ofs16;
}

static u16 __vpu_mbox_g_freesize(struct vpu_mailbox_h2f *mbox)
{
	u16 size16;
	u16 wptr_ofs16;
	u16 rptr_ofs16;

	wptr_ofs16 = mbox->wptr_ofs16;
	rptr_ofs16 = mbox->rptr_ofs16;

	size16 = wptr_ofs16 >= rptr_ofs16 ?
		max((u16)(mbox->data_size16 - wptr_ofs16), rptr_ofs16) :
		rptr_ofs16 - wptr_ofs16;

	return size16;
}

int vpu_mbox_ready(struct vpu_mailbox_ctrl *mctrl,
	size_t size, u32 type)
{
	u32 try_count;
	struct vpu_mailbox_h2f *mbox;
	u16 size16, free_size16;

	BUG_ON(!mctrl);
	BUG_ON(type >= VPU_MTYPE_H2F_CNT);
	BUG_ON(!IS_ALIGNED(size, 2));

	mbox = &mctrl->stack->h2f[type];
	size16 = size >> 1;
	try_count = 1000;

	free_size16 = __vpu_mbox_g_freesize(mbox);
	while (--try_count && (size16 > free_size16)) {
		vpu_warn("mbox(%d) is not ready(%d > %d)...(%d)\n", type,
			size16, free_size16, try_count);
		udelay(10);
		free_size16 = __vpu_mbox_g_freesize(mbox);
	}

	if (try_count)
		return 0;
	else
		return -EBUSY;
}

int vpu_mbox_write(struct vpu_mailbox_ctrl *mctrl,
	void *payload, size_t size, u32 type, u32 gid, u32 cmd, u32 cid)
{
	struct vpu_mailbox_h2f *mbox;
	struct vpu_mailbox_h2f_hdr *cmd_hdr;
	u16 size16;
	u16 wptr_ofs16;
	u16 rptr_ofs16;
	u16 *data, *wptr;

	BUG_ON(!mctrl);
	BUG_ON(!payload);
	BUG_ON(type >= VPU_MTYPE_H2F_CNT);
	BUG_ON(!IS_ALIGNED(size, 2));

	mbox = &mctrl->stack->h2f[type];
	data = vpu_h2f_g_mboxdata(mbox);
	wptr_ofs16 = mbox->wptr_ofs16;
	rptr_ofs16 = mbox->rptr_ofs16;
	size16 = size >> 1;

	wptr = data + wptr_ofs16;
	if ((wptr_ofs16 >= rptr_ofs16) && (mbox->data_size16 - wptr_ofs16 < size16)) {
		/* Write dummy message header */
		cmd_hdr = (struct vpu_mailbox_h2f_hdr *)wptr;
		cmd_hdr->signature = VPU_DUM_SIGNATURE;
		cmd_hdr->cid = VPU_DUM_SIGNATURE;
		cmd_hdr->size16 = mbox->data_size16 - wptr_ofs16;

#ifdef DBG_MAILBOX_CORE
		vpu_info("[I%d][MBOX][H2F%d] dummy(%d, %d, %d, %d)\n", gid, type,
			cid, wptr_ofs16, mbox->rptr_ofs16, cmd_hdr->size16);
#endif

		wptr = data;
		wptr_ofs16 = 0;
	}

	/* fill message header and copy message to Mail-box */
	cmd_hdr = (struct vpu_mailbox_h2f_hdr *)payload;
	cmd_hdr->signature = VPU_CMD_SIGNATURE;
	cmd_hdr->cid = cid;
	cmd_hdr->size16 = (__u16)size16;
	mem2iocpy(wptr, payload, size);

#ifdef DBG_MAILBOX_CORE
	vpu_info("[I%d][MBOX][H2F%d] %d command(%d, %d, %d, %d)\n", gid, type,
		cmd, cid, wptr_ofs16, mbox->rptr_ofs16, cmd_hdr->size16);
#endif

	wptr_ofs16 += size16;
	if (wptr_ofs16 == mbox->data_size16)
		wptr_ofs16 = 0;

	/* increment actual write pointer */
	mbox->wptr_ofs16 = wptr_ofs16;

	return 0;
}

int vpu_mbox_read(struct vpu_mailbox_ctrl *mctrl,
	void *payload, u32 type, void *debug_data)
{
	int ret = 0;
	struct vpu_mailbox_f2h *mbox;
	u16 *data, *rptr;

	BUG_ON(!mctrl);
	BUG_ON(!payload);
	BUG_ON(type >= VPU_MTYPE_F2H_CNT);

	mbox = &mctrl->stack->f2h[type];
	data = vpu_f2h_g_mboxdata(mbox);
	rptr = data + (mbox->msg_size16 * mbox->rmsg_idx);

	io2memcpy(payload, rptr, mbox->msg_size16 << 1);

#ifdef DBG_MAILBOX_CORE
	switch (type) {
	case VPU_MTYPE_F2H_NORMAL_RESPONSE:
	case VPU_MTYPE_F2H_LOW_RESPONSE:
	{
		struct vpu_interface *interface = debug_data;
		struct vpu_mailbox_reply *reply = payload;
		struct vpu_framemgr *iframemgr = &interface->framemgr;
		struct vpu_frame *iframe;
		u32 cid = reply->cid;

		iframe = &iframemgr->frame[cid];

		vpu_info("[I%ld][MBOX][F2H%d] %d reply(%d, %d, %d, %d)\n", iframe->param1, type,
			iframe->message, cid, reply->err_type, mbox->wmsg_idx, mbox->rmsg_idx);
		break;
	}
	case VPU_MTYPE_F2H_REPORT:
	{
		struct vpu_interface *interface = debug_data;
		struct vpu_mailbox_f2h_report *reply = payload;
		struct vpu_framemgr *iframemgr = &interface->framemgr;
		struct vpu_frame *iframe;
		u32 cid = reply->cid_lsb;

		iframe = &iframemgr->frame[cid];

		vpu_info("[I%ld][MBOX][F2H%d] %d reply(%d, %d, %d, %d)\n", iframe->param1, type,
			iframe->message, cid, reply->task_id, mbox->wmsg_idx, mbox->rmsg_idx);
		break;
	}
	default:
		break;
	}
#endif

	mbox->rmsg_idx = (mbox->rmsg_idx + 1) % mbox->emsg_idx;

	return ret;
}