summaryrefslogtreecommitdiff
path: root/bsp/allwinner/libraries/sunxi-hal/hal/source/g2d_rcq/g2d_wb.c
blob: fd74cb45a21db7bc143f5020bd524e05630b975e (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
/*
 * g2d_wb/g2d_wb.c
 *
 * Copyright (c) 2007-2019 Allwinnertech Co., Ltd.
 * Author: zhengxiaobin <[email protected]>
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * 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 <stdlib.h>
#include "g2d_wb.h"

static int wb_rcq_setup(struct wb_submodule *p_wb, u8 *base,
			struct g2d_rcq_mem_info *p_rcq_info)
{
	u8 *reg_base = base + G2D_WB;
	int ret = -1;

	if (!p_wb) {
		G2D_ERR_MSG("Null pointer!\n");
		goto OUT;
	}

	p_wb->reg_info->size = sizeof(struct g2d_mixer_write_back_reg);
	p_wb->reg_info->vir_addr = (u8 *)g2d_top_reg_memory_alloc(
	    p_wb->reg_info->size, (void *)&(p_wb->reg_info->phy_addr),
	    p_rcq_info);

	if (!p_wb->reg_info->vir_addr) {
		G2D_ERR_MSG("Malloc writeback reg rcq memory fail!\n");
		goto OUT;
	}

	p_wb->reg_blks->vir_addr = p_wb->reg_info->vir_addr;
	p_wb->reg_blks->phy_addr = p_wb->reg_info->phy_addr;
	p_wb->reg_blks->size = p_wb->reg_info->size;
	p_wb->reg_blks->reg_addr = reg_base;
	ret = 0;

OUT:
	return ret;
}

static __u32 wb_get_reg_block_num(struct wb_submodule *p_wb)
{
	if (p_wb)
		return p_wb->reg_blk_num;
	return 0;
}

static __s32 wb_get_reg_block(struct wb_submodule *p_wb,
			    struct g2d_reg_block **blks)
{
	__s32 i = 0, ret = -1;

	if (p_wb) {
		for (i = 0; i < p_wb->reg_blk_num; ++i)
			blks[i] = p_wb->reg_blks + i;
		ret = 0;
	}

	return ret;
}

static struct g2d_mixer_write_back_reg *wb_get_reg(struct wb_submodule *p_wb)
{
#if G2D_MIXER_RCQ_USED == 1
	return (struct g2d_mixer_write_back_reg *)(p_wb->reg_blks
						   ->vir_addr);
#else
	return (struct g2d_mixer_write_back_reg *)(p_wb->reg_blks
						   ->reg_addr);
#endif
	return NULL;
}


static void wb_set_block_dirty(struct wb_submodule *p_wb, __u32 blk_id, __u32 dirty)
{
#if G2D_MIXER_RCQ_USED == 1
	if (p_wb && p_wb->reg_blks->rcq_hd)
		p_wb->reg_blks->rcq_hd->dirty.bits.dirty = dirty;
	else
		G2D_ERR_MSG("Null pointer!\n");
#else

	if (p_wb)
		p_wb->reg_blks->dirty = dirty;
	else
		G2D_ERR_MSG("Null pointer!\n");
#endif
}

__s32 g2d_wb_set(struct wb_submodule *p_wb, g2d_image_enh *p_image)
{
	struct g2d_mixer_write_back_reg *p_reg = NULL;
	__u64 addr0, addr1, addr2;
	__u32 ycnt, ucnt, vcnt;
	__u32 pitch0, pitch1, pitch2;
	__u32 ch, cw, cy, cx;
	__s32 ret = -1;

	if (p_wb && p_image) {
		p_reg = p_wb->get_reg(p_wb);
		if (!p_reg)
			goto OUT;
		p_reg->wb_attr.bits.fmt = p_image->format;
		p_reg->data_size.bits.height =
		    (!p_image->clip_rect.h) ? 0 : p_image->clip_rect.h - 1;
		p_reg->data_size.bits.width =
		    (!p_image->clip_rect.w) ? 0 : p_image->clip_rect.w - 1;

		if ((p_image->format >= G2D_FORMAT_YUV422UVC_V1U1V0U0) &&
		    (p_image->format <= G2D_FORMAT_YUV422_PLANAR)) {
			cw = p_image->width >> 1;
			ch = p_image->height;
			cx = p_image->clip_rect.x >> 1;
			cy = p_image->clip_rect.y;
		} else if ((p_image->format >= G2D_FORMAT_YUV420UVC_V1U1V0U0) &&
			   (p_image->format <= G2D_FORMAT_YUV420_PLANAR)) {
			cw = p_image->width >> 1;
			ch = p_image->height >> 1;
			cx = p_image->clip_rect.x >> 1;
			cy = p_image->clip_rect.y >> 1;
		} else if ((p_image->format >= G2D_FORMAT_YUV411UVC_V1U1V0U0) &&
			   (p_image->format <= G2D_FORMAT_YUV411_PLANAR)) {
			cw = p_image->width >> 2;
			ch = p_image->height;
			cx = p_image->clip_rect.x >> 2;
			cy = p_image->clip_rect.y;
		} else {
			cw = 0;
			ch = 0;
			cx = 0;
			cy = 0;
		}

		g2d_byte_cal(p_image->format, &ycnt, &ucnt, &vcnt);
		pitch0 = cal_align(ycnt * p_image->width, p_image->align[0]);
		p_reg->pitch0 = pitch0;
		pitch1 = cal_align(ucnt * cw, p_image->align[1]);
		p_reg->pitch1 = pitch1;
		pitch2 = cal_align(vcnt * cw, p_image->align[2]);
		p_reg->pitch2 = pitch2;

		addr0 = p_image->laddr[0] + ((__u64)p_image->haddr[0] << 32) +
			pitch0 * p_image->clip_rect.y +
			ycnt * p_image->clip_rect.x;
		p_reg->laddr0 = addr0 & 0xffffffff;
		p_reg->haddr0 = (addr0 >> 32) & 0xff;
		addr1 = p_image->laddr[1] + ((__u64)p_image->haddr[1] << 32) +
			pitch1 * cy + ucnt * cx;
		p_reg->laddr1 = addr1 & 0xffffffff;
		p_reg->haddr1 = (addr1 >> 32) & 0xff;
		addr2 = p_image->laddr[2] + ((__u64)p_image->haddr[2] << 32) +
			pitch2 * cy + vcnt * cx;
		p_reg->laddr2 = addr2 & 0xffffffff;
		p_reg->haddr2 = (addr1 >> 32) & 0xff;
		p_wb->set_block_dirty(p_wb, 0, 1);
	}

OUT:
	return ret;
}

static __u32 wb_get_rcq_mem_size(struct wb_submodule *p_wb)
{
	return G2D_RCQ_BYTE_ALIGN(sizeof(struct g2d_mixer_write_back_reg));
}

static __s32 wb_destory(struct wb_submodule *p_wb)
{
	__s32 ret = -1;

	if (p_wb) {
		free(p_wb->reg_blks);
		p_wb->reg_blks = NULL;

		free(p_wb->reg_info);
		p_wb->reg_info = NULL;
		ret = 0;
		free(p_wb);
	}

	return ret;
}

struct wb_submodule *g2d_wb_submodule_setup(struct g2d_mixer_frame *p_frame)
{
	struct wb_submodule *p_wb = NULL;

	p_wb = hal_malloc(sizeof(struct wb_submodule));

	if (!p_wb) {
		G2D_ERR_MSG("malloc wb submodule fail!\n");
		return NULL;
	}
	memset(p_wb, 0, sizeof(struct wb_submodule));
	p_wb->rcq_setup = wb_rcq_setup;
	p_wb->reg_blk_num = 1;
	p_wb->get_reg_block_num = wb_get_reg_block_num;
	p_wb->get_reg_block = wb_get_reg_block;
	p_wb->get_reg = wb_get_reg;
	p_wb->set_block_dirty = wb_set_block_dirty;
	p_wb->get_rcq_mem_size = wb_get_rcq_mem_size;
	p_wb->destory = wb_destory;

	p_wb->reg_blks =
	    hal_malloc(sizeof(struct g2d_reg_block) * p_wb->reg_blk_num);
	p_wb->reg_info =
	    hal_malloc(sizeof(struct g2d_reg_mem_info));

	if (!p_wb->reg_blks || !p_wb->reg_info) {
		G2D_ERR_MSG("Kmalloc wb reg info fail!\n");
		goto FREE_WB;
	}
	memset(p_wb->reg_blks, 0, sizeof(struct g2d_reg_block) * p_wb->reg_blk_num);
	memset(p_wb->reg_info, 0, sizeof(struct g2d_reg_mem_info));

	return p_wb;
FREE_WB:
	free(p_wb->reg_blks);
	free(p_wb->reg_info);
	free(p_wb);

	return NULL;
}