cedrus.c 4.31 KB
Newer Older
Jens Kuske's avatar
Jens Kuske committed
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
/*
 * Copyright (c) 2013-2016 Jens Kuske <jenskuske@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include <fcntl.h>
#include <pthread.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include "cedrus.h"
#include "cedrus_mem.h"
#include "cedrus_regs.h"
#include "kernel-headers/cedardev_api.h"

#define DEVICE "/dev/cedar_dev"
#define EXPORT __attribute__ ((visibility ("default")))

static struct cedrus
{
	int fd;
	void *regs;
	int version;
	int ioctl_offset;
	struct cedrus_allocator *allocator;
	pthread_mutex_t device_lock;
} ve = { .fd = -1, .device_lock = PTHREAD_MUTEX_INITIALIZER };

EXPORT struct cedrus *cedrus_open(void)
{
	if (ve.fd != -1)
		return NULL;

	struct cedarv_env_infomation info;

	ve.fd = open(DEVICE, O_RDWR);
	if (ve.fd == -1)
		return NULL;

	if (ioctl(ve.fd, IOCTL_GET_ENV_INFO, (void *)(&info)) == -1)
		goto close;

	ve.regs = mmap(NULL, 0x800, PROT_READ | PROT_WRITE, MAP_SHARED, ve.fd, info.address_macc);
	if (ve.regs == MAP_FAILED)
		goto close;

Jens Kuske's avatar
Jens Kuske committed
65
66
#ifdef USE_UMP
	ve.allocator = cedrus_allocator_ump_new();
Jens Kuske's avatar
Jens Kuske committed
67
	if (!ve.allocator)
Jens Kuske's avatar
Jens Kuske committed
68
#endif
Jens Kuske's avatar
Jens Kuske committed
69
	{
Jens Kuske's avatar
Jens Kuske committed
70
		ve.allocator = cedrus_allocator_ve_new(ve.fd, &info);
Jens Kuske's avatar
Jens Kuske committed
71
		if (!ve.allocator)
Jens Kuske's avatar
Jens Kuske committed
72
73
74
75
76
		{
			ve.allocator = cedrus_allocator_ion_new();
			if (!ve.allocator)
				goto unmap;
		}
Jens Kuske's avatar
Jens Kuske committed
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
	}

	ioctl(ve.fd, IOCTL_ENGINE_REQ, 0);

	ve.version = readl(ve.regs + VE_VERSION) >> 16;

	if (ve.version >= 0x1667)
		ve.ioctl_offset = 1;

	ioctl(ve.fd, IOCTL_ENABLE_VE + ve.ioctl_offset, 0);
	ioctl(ve.fd, IOCTL_SET_VE_FREQ + ve.ioctl_offset, 320);
	ioctl(ve.fd, IOCTL_RESET_VE + ve.ioctl_offset, 0);

	writel(0x00130007, ve.regs + VE_CTRL);

	return &ve;

unmap:
	munmap(ve.regs, 0x800);
close:
	close(ve.fd);
	ve.fd = -1;
	return NULL;
}

EXPORT void cedrus_close(struct cedrus *dev)
{
	if (dev->fd == -1)
		return;

	ioctl(dev->fd, IOCTL_DISABLE_VE + dev->ioctl_offset, 0);
	ioctl(dev->fd, IOCTL_ENGINE_REL, 0);

	munmap(dev->regs, 0x800);
	dev->regs = NULL;

	dev->allocator->free(dev->allocator);

	close(dev->fd);
	dev->fd = -1;
}

EXPORT int cedrus_get_ve_version(struct cedrus *dev)
{
	if (!dev)
		return 0x0;

	return dev->version;
}

EXPORT int cedrus_ve_wait(struct cedrus *dev, int timeout)
{
	if (!dev)
		return -1;

	return ioctl(dev->fd, IOCTL_WAIT_VE_DE, timeout);
}

EXPORT void *cedrus_ve_get(struct cedrus *dev, enum cedrus_engine engine, uint32_t flags)
{
	if (!dev || pthread_mutex_lock(&dev->device_lock))
		return NULL;

	writel(0x00130000 | (engine & 0xf) | (flags & ~0xf), dev->regs + VE_CTRL);

	return dev->regs;
}

EXPORT void cedrus_ve_put(struct cedrus *dev)
{
	if (!dev)
		return;

	writel(0x00130007, dev->regs + VE_CTRL);
	pthread_mutex_unlock(&dev->device_lock);
}

EXPORT struct cedrus_mem *cedrus_mem_alloc(struct cedrus *dev, size_t size)
{
	if (!dev || size == 0)
		return NULL;

	return dev->allocator->mem_alloc(dev->allocator, (size + 4096 - 1) & ~(4096 - 1));
}

EXPORT void cedrus_mem_free(struct cedrus_mem *mem)
{
	if (!mem)
		return;

	ve.allocator->mem_free(ve.allocator, mem);
}

EXPORT void cedrus_mem_flush_cache(struct cedrus_mem *mem)
{
	if (!mem)
		return;

	ve.allocator->mem_flush(ve.allocator, mem);
}

EXPORT void *cedrus_mem_get_pointer(const struct cedrus_mem *mem)
{
	if (!mem)
		return NULL;

	return mem->virt;
}

EXPORT uint32_t cedrus_mem_get_phys_addr(const struct cedrus_mem *mem)
{
	if (!mem)
		return 0x0;

	return mem->phys;
}

uint32_t phys2bus(uint32_t phys)
{
	return phys - 0x40000000;
}

uint32_t bus2phys(uint32_t bus)
{
	return bus + 0x40000000;
}

EXPORT uint32_t cedrus_mem_get_bus_addr(const struct cedrus_mem *mem)
{
	if (!mem)
		return 0x0;

	return phys2bus(mem->phys);
}